-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Though we are filtering out for undefined values, typescript is not able to recognize this. #57457
Comments
This is a duplicate of many issues. It works as intended. Typescript only narrows the output of You can define a typeguard that checks that the value is not undefined and then use that: function isDefined<T>(x: T | undefined): x is T {
return x !== undefined;
}
const foo = [1, undefined, 2];
const foo2: number[] = foo.filter(isDefined); |
I didn't know GitHub had image search π |
On a semi-related note, it'd be nice if the return type of a type predicate were transitive. At the moment, this doesn't work:
because even thought the type of
The repetition should be unnecessary. |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
π Version & Regression Information
β― Playground Link
No response
π» Code
π Actual behavior
Show this error in 'foo2' :
Type '(number | undefined)[]' is not assignable to type 'number[]'.
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
π Expected behavior
Should not show this any error, because we are filtering out all undefined.
Additional information about the issue
The text was updated successfully, but these errors were encountered: