Skip to content

Inconsistent inferred type predicate for filterΒ #61182

Closed as not planned
Closed as not planned
@pedrolamas

Description

@pedrolamas

πŸ”Ž Search Terms

filter type predicate

πŸ•— Version & Regression Information

This only makes sense since TS5.5, when inferred type predicates were introduced.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/MYewdgzgLgBAhjAvDA2gcigU2mgNDMAVwBtiBdAKAHoqY6YA9AfgotElgCMl4A6AMwCWxLACcAFAA8kAPhjSAhImRFSASmq16zVu2gxgPOAOFips+TCUqSxGADJ783tDiioEAOqCoAC3EYaGoaNPSMLBRAA

πŸ’» Code

const a = ['test', null]

const b = a.filter(x => x !== null)

const c = a.filter(x => x !== null && x.startsWith('t'))

πŸ™ Actual behavior

On the above, the type of b is correctly inferred as string[], but the type of c is incorrectly inferred to (string | null)[]

πŸ™‚ Expected behavior

The inferred type of c should string[], just like the inferred type of b.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions