Skip to content

Typescript doesn't recognize null check in previous array.filter(). #51384

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

Closed
DrowningElysium opened this issue Nov 2, 2022 · 1 comment
Closed
Labels
Duplicate An existing issue was already created

Comments

@DrowningElysium
Copy link

In a situation a different user has written code that puts the email of a client to the id field as this is needed for the way the multiselect is written. The problem I am facing is that in the interface I have made the type of the email field string | null as we do have clients that haven't yet fully given their information yet. In the old situation they didn't respect a possible null value.

Because of that I thought of trying to fix this with a filter.

const contacts =
  thisClient.value?.contacts
    ?.filter(contact => props.form.contactAttendeeIds.includes(contact.id))
    .filter(contact => contact.email !== null)
    // Typescript still doesn't recognize that this can't be null anymore.
    .map(contact => ({...contact, ...{id: contact.email as string}})) ?? [];

Because it doesn't recognize that the value can no longer be null it still throws type errors in our pipeline.
It this something that can be fixed or a problem I need to tackle differently?

@DrowningElysium DrowningElysium added the Duplicate An existing issue was already created label Nov 2, 2022
@MartinJohns
Copy link
Contributor

Duplicate of #38390.

Just add a type annotation to make your function a type guard, otherwise it's not used to narrow.

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

2 participants