Fix TaggedUnion constraint to allow documented generic usage#1430
Open
ATOM00blue wants to merge 1 commit into
Open
Fix TaggedUnion constraint to allow documented generic usage#1430ATOM00blue wants to merge 1 commit into
ATOM00blue wants to merge 1 commit into
Conversation
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
There was a problem hiding this comment.
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’sUnionMembersconstraint fromRecord<string, Record<string, unknown>>toRecord<string, unknown>. - Updated the documentation example to match the intended generic-wrapper constraint.
- Added a
test-dcase 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.
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.
The documented usage of
TaggedUnion,failed to compile with:
Type 'Fields' does not satisfy the constraint 'Record<string, Record<string, unknown>>'.The
UnionMembersparameter requiredRecord<string, Record<string, unknown>>— a redundant extra level ofRecord. A wrapper constrained as the naturalRecord<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