Add const type parameters for literal type inference#425
Merged
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds TypeScript 5.0's
consttype parameters to thetypeChecker,isOptionalOf,isArrayOf,isDictionaryOf, andensurefunctions. 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 genericstringornumbertypes.Key Changes
constmodifier to type parameters intypeChecker(),isOptionalOf(),isArrayOf(),isDictionaryOf(), andensure()functionsconstTypeParams.test.ts) with:Implementation Details
The
const Tmodifier ensures that when a Set likenew Set(["a", "b"])is passed to these functions, TypeScript infers the type as the literal union"a" | "b"rather than the widened typestring. This provides better type safety and more precise type checking at compile time while maintaining full runtime compatibility.https://claude.ai/code/session_014UV4gEoodBgPAE8d4jcoJ8