Skip to content

Fix TaggedUnion constraint to allow documented generic usage#1430

Open
ATOM00blue wants to merge 1 commit into
sindresorhus:mainfrom
ATOM00blue:fix/taggedunion-constraint
Open

Fix TaggedUnion constraint to allow documented generic usage#1430
ATOM00blue wants to merge 1 commit into
sindresorhus:mainfrom
ATOM00blue:fix/taggedunion-constraint

Conversation

@ATOM00blue

Copy link
Copy Markdown

The documented usage of TaggedUnion,

type Tagged<Fields extends Record<string, unknown>> = TaggedUnion<'type', Fields>;

failed to compile with: Type 'Fields' does not satisfy the constraint 'Record<string, Record<string, unknown>>'.

The UnionMembers parameter required Record<string, Record<string, unknown>> — a redundant extra level of Record. A wrapper constrained as the natural Record<string, unknown> cannot satisfy the nested constraint, so the documented pattern (and any generic passthrough) was rejected.

This loosens the constraint to Record<string, unknown>. Per-member field types are still fully enforced by the & UnionMembers[Name] intersection in the mapped type, so the existing negative tests continue to pass. Added a test mirroring the documented pattern.

Closes #1211

The `UnionMembers` constraint required `Record<string, Record<string,
unknown>>`, but the documented `Tagged<Fields extends Record<string,
unknown>>` wrapper could not satisfy the redundant nested `Record`,
failing with "Type 'Fields' does not satisfy the constraint". Loosen it
to `Record<string, unknown>`; field-value type-checking is still enforced
by the `& UnionMembers[Name]` intersection.

Closes sindresorhus#1211
Copilot AI review requested due to automatic review settings May 21, 2026 03:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates TaggedUnion to support being wrapped by generics constrained as Record<string, unknown> (as documented), and adds type-level tests to prevent regressions.

Changes:

  • Widened TaggedUnion’s UnionMembers constraint from Record<string, Record<string, unknown>> to Record<string, unknown>.
  • Updated the documentation example to match the intended generic-wrapper constraint.
  • Added a test-d case to validate the wrapper scenario and correct narrowing behavior.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.

File Description
test-d/tagged-union.ts Adds a regression test ensuring TaggedUnion works through a generic wrapper constrained as Record<string, unknown>.
source/tagged-union.d.ts Widens the TaggedUnion type constraint and updates the docs example accordingly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

TaggedUnion cannot be used in the manner described in the documentation

2 participants