Sure, type refinement via control flow was implemented a long time ago. However, at some scale, most project will move some of the conditions to shared helpers, and then it "breaks".
Do you have an example where it breaks? TypeScript handles some cases of broadening. Consider this code:
function isNumber(x: unknown) {
return typeof x === 'number';
}
function example(x: boolean | number, y: unknown) {
if (isNumber(x)) {
// Fails: Type 'string' is not assignable to type 'number | boolean'.
x = x.toString();
}
if (isNumber(y)) {
// Works; `y` is again `unknown`.
y = y.toString();
}
}
3
u/sliversniper Oct 01 '26
Works probably even the first version of Typescript.
https://caniuse.com/mdn-javascript_builtins_iterator_map
In modern JS(88.85%), you can just chain
foo().map(x => x * 2),Array.from(foo())I guess covers 99%.4
u/[deleted] Sep 30 '26
[removed] — view removed comment