-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Milestone
Description
Discriminated union types are limited to only one literal type property. This can be very useful sometimes when the same of objects are complex.
interface A1 {
type: 'a';
subtype: 1;
}
interface A2 {
type: 'a';
subtype: 2;
foo: number;
}
interface B {
type: 'b';
}
type AB = A1 | A2 | B;
const ab: AB = <AB>{};
if (ab.type === 'a') {
// ab is A1 | A2, awesome! ♥
if (ab.subtype === 2) {
// ab is still A1 | A2 :( it should be A2
ab.foo; // Error: foo doesn't exist on A1 | A2
}
}
Expected behavior:
compile successful.
Actual behavior:
won't compile without explicit cast.
rozzzly and svieira
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue