You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use Array.filter(Boolean) to filter out falsy values, e.g. nulls and empty strings from a string|null array. The result should be a string array. I am using strict mode (strictNullChecks).
The Array interface could be extended with something like this:
Duplicate of #16655 I believe. Which was included and then reverted due to it causing breaking changes. Comment
This was reverted due to it causing a breaking change in 3.5. The simple repro looks like this:
declareconstBullean: {new(value?: any): Boolean;<T>(value?: T): value is Exclude<T,false|null|undefined|''|0>;};declareconstarr: any[];// Was any[], now (3.5) unknown[]constarr2=arr.filter(Bullean);
There might be something wrong in generic inference, but it's not yet clear. We'll need this scenario to work before adding it back in.
I want to use Array.filter(Boolean) to filter out falsy values, e.g. nulls and empty strings from a string|null array. The result should be a string array. I am using strict mode (strictNullChecks).
The Array interface could be extended with something like this:
TypeScript Version: 3.4.5
Search Terms: array filter boolean narrow
Code
Expected behavior:
Type of a2 should be string[].
Actual behavior:
Type of a2 is (string|null)[].
Playground Link: https://www.typescriptlang.org/play/index.html#src=const%20a1%20%3D%20%5B%22a%22%2C%20null%2C%20%22b%22%2C%20%22%22%5D%3B%0D%0Aconst%20a2%20%3D%20a1.filter(Boolean)%3B%0D%0Aconst%20s%20%3D%20a2%5B0%5D.toUpperCase()%3B%0D%0A
The text was updated successfully, but these errors were encountered: