Assignability errors for discriminated unions now check the value of the discriminant to decide which member of the union to check for assignability. Previously, assignability didn't know about discriminated unions and would check every member, issuing errors for the last member of the union if assignability failed. For example: ```ts type Square = { kind: "sq", size: number } type Rectangle = { kind: "rt", x: number, y: number } type Circle = { kind: "cr", radius: number } type Shape = | Square | Rectangle | Circle; let shape: Shape = { kind: "sq", x: 12, y: 13, } ``` `typeRelatedToSomeType` now checks whether each property in the source type is a discriminant. It finds `kind` and proceeds to look for the type in the target union that has `kind: "sq"`. If it finds it, which it does in this example (`Square`), then it checks only assignbility to `Square`. The result is that the error now says that property 'size' is missing in type `{ kind: "sq", x: number, y: number }` instead of saying that that "sq" is not assignable to type "cr" like it did before. Fixes #10867
13 lines
259 B
TypeScript
13 lines
259 B
TypeScript
type Square = { kind: "sq", size: number }
|
|
type Rectangle = { kind: "rt", x: number, y: number }
|
|
type Circle = { kind: "cr", radius: number }
|
|
type Shape =
|
|
| Square
|
|
| Rectangle
|
|
| Circle;
|
|
let shape: Shape = {
|
|
kind: "sq",
|
|
x: 12,
|
|
y: 13,
|
|
}
|