Skip to content

Use NoInfer in GetTagMetadata to prevent inference widening - #1418

Open
tamish-max wants to merge 1 commit into
sindresorhus:mainfrom
tamish-max:fix/use-noinfer-848
Open

tamish-max wants to merge 1 commit into
sindresorhus:mainfrom
tamish-max:fix/use-noinfer-848

Conversation

@tamish-max

Copy link
Copy Markdown

Closes #848

Summary

After auditing all exported types in source/, GetTagMetadata is the only type where NoInfer (TS 5.4+) is applicable. TagName appears in both the constraint (Tag<TagName, unknown>) and as a standalone parameter, creating competing inference sites. When Type carries multiple tags, TypeScript could widen TagName from the structural shape.

Change

 export type GetTagMetadata<
-  Type extends Tag<TagName, unknown>,
+  Type extends Tag<NoInfer<TagName>, unknown>,
   TagName extends PropertyKey
 > = Type[typeof tag][TagName];

Why only GetTagMetadata?

All other types were reviewed. NoInfer requires: (1) a type param in a constraint AND as a standalone param, (2) competing inference sites that can conflict. Most type-fest types use extends infer for computation captures or have single inference sites — neither pattern benefits from NoInfer.

Test

Added multi-tag test in test-d/opaque.ts verifying precise metadata recovery when a type carries multiple tags.

Note: Draft PR #1403 proposes the same change but lacks tests. This PR adds the regression test.

Closes sindresorhus#848

After auditing all exported types, GetTagMetadata is the only type where
NoInfer applies — TagName appears in both the constraint (Tag<TagName, unknown>)
and as a standalone parameter, creating competing inference sites.

When Type carries multiple tags, TypeScript could widen TagName from the
structural shape of Type. NoInfer suppresses this, ensuring TagName is
inferred only from the explicit argument.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use NoInfer?

1 participant