microsoft / microsoft/TypeScript
Incorrect error message on invalid union type assignation
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
error message union type assignation
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
💻 Code
const c: {x : 'a' | 'b' | 'c'} = {x:'c'}
const b: {x: 'a'} | {x: 'b'} = c;
🙁 Actual behavior
The error message is
Type '{ x: "a" | "b" | "c"; }' is not assignable to type '{ x: "a"; } | { x: "b"; }'.
Type '{ x: "a" | "b" | "c"; }' is not assignable to type '{ x: "b"; }'.
Types of property 'x' are incompatible.
Type '"a" | "b" | "c"' is not assignable to type '"b"'.
Type '"a"' is not assignable to type '"b"'.(2322)
The problem starts with the line Type '{ x: "a" | "b" | "c"; }' is not assignable to type '{ x: "b"; }'.. It is true that the types are incompatible, but it is not true that this is the source of the error.
In this simple case, it's easy to see where the actual problem is, but with large unions, it becomes hard to know where the error comes from.
🙂 Expected behavior
The actual problem is that type 'c' is not assignable to 'a' | 'b', and the error message should mention it. Maybe the full trace should look like this:
Type '{ x: "a" | "b" | "c"; }' is not assignable to type '{ x: "a"; } | { x: "b"; }'.
Types of property 'x' are incompatible.
Type '"a" | "b" | "c"' is not assignable to type '"a" | "b"'.
Type '"c"' is not assignable to type '"a" | "b"'.(2322)
Additional information about the issue
When the unions are large and aliased, the misleading message makes errors hard to debug.
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 by reproducing the supplied union example in the TypeScript Playground link and inspect the TS2322 diagnostic. No repository file or test is named; done means the diagnostic identifies type "c" as not assignable to "a" | "b" rather than reporting only the misleading union branch.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100