Hacker News new | past | comments | ask | show | jobs | submit login

The missing feature I want most is type narrowing across chained functions like:

arr.filter(a => a.kind === „bar“).map(a => /* a should now be of type Bar just like in a branch */)




It was would be nice for this to be automatic but filter does work like this it you pass a type guard as the predicate:

``` arr.filter((a: A): a is A & { kind: 'bar'} => a.kind === 'bar') ```

If you define:

``` function isKind<K extends A['kind']>(kind: K): (a: A) => a is A & { kind: K } { return (a): a is A & { kind: K } => a.kind === kind; } ```

Then you can write:

``` arr.filter(isKind('bar')) ```

and get a properly narrowed return type array


Nice, thank you



Wait, are you saying they added that in this release? That's awesome!


This is not part of this release.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: