Skip to content
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

Closed
JosephJibi opened this issue Feb 21, 2024 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@JosephJibi
Copy link

πŸ”Ž Search Terms

image

πŸ•— Version & Regression Information

  • This is a crash
  • I was unable to test this on prior versions.

⏯ Playground Link

No response

πŸ’» Code

    const foo = [1, undefined, 2];
    const foo2: number[] = foo.filter((value) => value);

πŸ™ 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

image
image

@MichalMarsalek
Copy link

MichalMarsalek commented Feb 21, 2024

This is a duplicate of many issues. It works as intended. Typescript only narrows the output of filter if the predicate is a typeguard, which (value) => value is not.

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);

@bgenia
Copy link

bgenia commented Feb 21, 2024

see #16069 #38390

@fatcerberus
Copy link

Search Terms: [image]

I didn't know GitHub had image search πŸ˜‰

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 21, 2024
@tomkcook
Copy link

tomkcook commented Feb 22, 2024

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:

class Foo {
  foo?: string;

  isComplete(): this is CompleteFoo { return this.foo !== undefined; }
}

interface CompleteFoo extends Foo {
  foo: string;
}

const arrayOfFoo: Foo[] = [];
const completeFoos = arrayOfFoo.filter<CompleteFoo>((foo) => foo.isComplete());

because even thought the type of Foo.prototype.isComplete is declared as this is CompleteFoo, when it's called that type collapses to boolean and the compiler has to be told again that it's a type predicate:

const completeFoos = arrayOfFoo.filter<CompleteFoo>((foo): foo is CompleteFoo => foo.isComplete());

The repetition should be unnecessary.

@fatcerberus
Copy link

fatcerberus commented Feb 22, 2024

@tomkcook That should be solved by #57465

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

7 participants