Skip to content

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

Closed
@DrowningElysium

Description

@DrowningElysium

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions