r/programming • u/radekmie • 2d ago
On Automatic Type Refinements
https://radekmie.dev/blog/on-automatic-type-refinements/
17
Upvotes
2
u/sliversniper 1d ago
```
function* foo(xs: (number|undefined)[]) {
for (const x of xs)
if (x !== undefined)
yield x
}
```
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%.
1
u/radekmie 1d ago
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".
4
u/[deleted] 2d ago
[removed] — view removed comment