Skip to content

Filter narrowing down its output type without type guards #30863

Closed
@TomasHubelbauer

Description

@TomasHubelbauer

I have come across issues such as #7657, #20812 and #20218 as well as SO questions such as this one, but they all deal with type guards.

Could it be made so that the following code:

const items: (number | null)[] = [1,2,3,null];
const numbers = items.filter(i => i !== null);

…just works - infers that numbers is number[] with --strictNullChecks on? I think this should be out of the box behavior with TypeScript and lib.d.ts typings. Is this possible?

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