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
When operating on an array, it's quite common to filter it to ensure there are no null or undefined values. Unfortunately, this doesn't narrow the type further. This means that if you subsequently use the value in strict mode, you have to either cast it to a non-nullable type, or use the non-null assertion operator.
I would like to suggest the ability to use the ! operator on the function arguments themselves, rather than on every scenario when you use the value.
constarray1: Array<{test: string}|undefined>=[];// Won't compile as x is possibly undefinedarray1.filter(x=>!!x).map(x=>x.test);// Current fix:array1.filter(x=>!!x).map(x=>x!.test);// Or:array1.filter(x=>!!x).map((x: {test: string})=>x.test);// SUGGESTION: This would be better because you can use x multiple times without adding ! every time:array1.filter(x=>!!x).map(x!=>x.test);
The text was updated successfully, but these errors were encountered:
I think you need some type-predicates to narrow the type:
declareconstarray1: Array<{test: string}|undefined>;declarefunctionis_non_null<T>(value: T|undefined): value is T;array1.filter(is_non_null).map(x=>x.test);// no error ^ x === { test: string }
I don't think we'd be keen to add new punctuation to function parameters unless it was extremely compelling. There are a lot of other ways to fix this specific scenario
When operating on an array, it's quite common to filter it to ensure there are no null or undefined values. Unfortunately, this doesn't narrow the type further. This means that if you subsequently use the value in strict mode, you have to either cast it to a non-nullable type, or use the non-null assertion operator.
I would like to suggest the ability to use the ! operator on the function arguments themselves, rather than on every scenario when you use the value.
The text was updated successfully, but these errors were encountered: