Use NoInfer utility type where applicable#1403
Draft
whatfontisthis wants to merge 1 commit into
Draft
Conversation
…'s structure The constraint `Type extends Tag<TagName, unknown>` creates an additional inference site for `TagName` — TypeScript can infer it from the structural shape of `Type` (e.g., which tags it carries) rather than solely from the explicit `TagName` argument. Wrapping with `NoInfer<TagName>` in the constraint position ensures that when `GetTagMetadata` is used in generic function signatures, `TagName` is inferred only from the explicit type argument, not from the tag structure embedded in `Type`. This matches the intended semantics: the caller should supply the tag name they want to look up; the shape of the tagged value should validate that lookup, not drive it. Closes sindresorhus#848 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
Addresses #848 by applying TypeScript 5.4's
NoInferutility type where it genuinely improves inference behavior.Changes
GetTagMetadatainsource/tagged.d.tsBefore:
After:
Why
NoInferhelps here:The constraint
Type extends Tag<TagName, unknown>creates two potential inference sites forTagName:Type— sinceTypemust extendTag<TagName, unknown>, TypeScript can inferTagNameby inspecting which tagsTypecarries.TagNameargument itself.When
GetTagMetadatais used in a generic function that accepts both a tagged value and a tag name:Without
NoInfer, TypeScript may use the structure ofTas an inference site forK, potentially wideningKwhenTcarries multiple tags. WithNoInfer<TagName>in the constraint,Kis inferred only from the explicittagNameargument — which is the intended semantics. The tagged value's structure should validate the lookup, not determine the tag name.What was reviewed
All source types in
source/were reviewed forNoInferapplicability. Most type-fest types are pure type-level utilities that don't involve competing inference sites for the same type parameter —NoInferis primarily beneficial in function generics, and most type-fest types are used as explicit type arguments rather than inferred from multiple argument positions simultaneously.GetTagMetadatais the clearest case: it has both a constraint-based inference site and an explicit parameter for the sameTagNamevariable, makingNoInfersemantically correct.Tests
All existing tests pass:
test-d/opaque.ts— coversTagged,GetTagMetadata,UnwrapTagged, etc.tsc --noEmit) passes with no errors.Closes #848