microsoft / microsoft/TypeScript
Type parameter not usable as type argument in identical type; union in constraint becomes intersection
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
indexed access, constraints, generic, unions, ts2344
🕗 Version & Regression Information
- This changed between versions 3.3 and 3.5 (likely #30769)
⏯ Playground Link
💻 Code
interface Foo<T extends { x: string }, U extends (T | { x: "a" })['x']> {
z: Foo<T, U> // error!
// ~ Type 'U' does not satisfy the constraint 'T["x"] & "a"'. 🙃
}
🙁 Actual behavior
The U type parameter is rejected as a type argument with a TS2344 error about how it doesn't satisfy the constraint. The constraint seems to have shifted from a union to an intersection, even though it comes from an identical place.
🙂 Expected behavior
The U type parameter should be accepted as a type argument.
Additional information about the issue
Distilled from SO question.
I expect this is a consequence of #30769 as per https://github.com/microsoft/TypeScript/issues/31731#issuecomment-498465358, but it is at least somewhat surprising that this should happen when the types involved are identical. One could rewrite the constraint to U extends T["x"] | "a", of course, but this is distilled from the above SO question which is presumably distilled from some use case. What's happening here, exactly, and is it intended, a design limitation, or a bona fide bug?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground and the minimal generic constraint example in the issue. Investigate why the indexed-access constraint is treated as an intersection rather than the expected union, considering the reported regression between versions 3.3 and 3.5 and related issue #30769. Done means determining whether this is intended or correcting the behavior so U is accepted without TS2344.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100