Skip to content

Add const type parameters for literal type inference#425

Merged
gjbkz merged 1 commit intomainfrom
claude/typescript-const-type-params-DhvVy
Feb 19, 2026
Merged

Add const type parameters for literal type inference#425
gjbkz merged 1 commit intomainfrom
claude/typescript-const-type-params-DhvVy

Conversation

@gjbkz
Copy link
Member

@gjbkz gjbkz commented Feb 19, 2026

Summary

This PR adds TypeScript 5.0's const type parameters to the typeChecker, isOptionalOf, isArrayOf, isDictionaryOf, and ensure functions. This enables the compiler to infer the most specific (literal) types for type arguments instead of widening them, allowing Set-based definitions to produce literal union types rather than generic string or number types.

Key Changes

  • Added const modifier to type parameters in typeChecker(), isOptionalOf(), isArrayOf(), isDictionaryOf(), and ensure() functions
  • Added comprehensive test file (constTypeParams.test.ts) with:
    • Compile-time type assertions verifying literal union type inference
    • Runtime tests validating the behavior of all modified functions
    • Tests covering string literals, numeric literals, and optional/array/dictionary variants

Implementation Details

The const T modifier ensures that when a Set like new Set(["a", "b"]) is passed to these functions, TypeScript infers the type as the literal union "a" | "b" rather than the widened type string. This provides better type safety and more precise type checking at compile time while maintaining full runtime compatibility.

https://claude.ai/code/session_014UV4gEoodBgPAE8d4jcoJ8

…efinitions

TypeScript 5.0 introduced `const` type parameters which prevent type widening
during inference. Adding `const T` to `typeChecker`, `isOptionalOf`,
`isArrayOf`, `isDictionaryOf`, and `ensure` allows TypeScript to infer literal
union types when a `Set` of literals is passed as a definition.

Before this change:
  const isColor = typeChecker(new Set(["red", "green", "blue"]));
  // TypeChecker<string>  ← literal types lost

After this change:
  const isColor = typeChecker(new Set(["red", "green", "blue"]));
  // TypeChecker<"red" | "green" | "blue">  ← literals preserved

A new test file (src/constTypeParams.test.ts) contains both compile-time type
assertions (verified by tsc) and runtime checks covering all four combinators.

https://claude.ai/code/session_014UV4gEoodBgPAE8d4jcoJ8
@codecov
Copy link

codecov bot commented Feb 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.19%. Comparing base (cae2eb5) to head (813eebe).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #425      +/-   ##
==========================================
+ Coverage   96.86%   97.19%   +0.33%     
==========================================
  Files          45       45              
  Lines        2104     2107       +3     
  Branches      209      210       +1     
==========================================
+ Hits         2038     2048      +10     
+ Misses         66       59       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gjbkz gjbkz merged commit de90936 into main Feb 19, 2026
8 checks passed
@gjbkz gjbkz deleted the claude/typescript-const-type-params-DhvVy branch February 19, 2026 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments