|
|
|
|
|
by amitport
1629 days ago
|
|
I wonder if it is possible to add a feature to Typescript to help with this: You could potentially add a syntax for type guards function types, then add a signature to filter that accepts a type guard and returns an array of the guarded types. Shouldn't be too much of a stretch given that we have type guards. The syntax is a bit annoying... should be something like filter<A, B>(cb: A => A is B) :/ |
|
I don't know why the type isn't narrowed in Array.filter like it is in if statements without this weird workaround.
This example in Typescript playground: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAhgJwXA...[1]: https://www.typescriptlang.org/docs/handbook/advanced-types....