Created
January 2, 2022 16:06
-
-
Save kasperpeulen/19ab815171ee44f354f5e3e1abb89c34 to your computer and use it in GitHub Desktop.
New control flow analysis improvements in typescript are quite mind blowing...
This file contains 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
type MyObject = { a: 1; b: 2; c: 3 } | { a: 4; b: 5; c: 6 } | { a: 7; b: 8; c: 9 }; | |
declare const object: MyObject; | |
const { a, b, c } = object; | |
if (a === 1) { | |
console.log(object); // type is {a: 1, b: 2, c: 3} (since 4.4) | |
} else if (a === 2) { | |
// TS2367: This condition will always return 'false' since the types '4 | 7' and '2' have no overlap. | |
console.log(object); // type inferred as never | |
} else if (b === 5) { | |
console.log(object); // type inferred {a: 4, b: 5, c: 6} (since 4.4) | |
console.log(a); // type inferred as 1 (will be in 4.6) | |
console.log(c); // type inferred as 3 (will be in 4.6) | |
} else if (c === 9) { | |
console.log(object); // type inferred as {a: 7, b: 8, c: 9} | |
console.log([a, b]); // type inferred as the tuple [7, 9] (will be in 4.6) | |
} else { | |
console.log(object); // type inferred as never | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment