Skip to content

Inferred Type Predicates doesn't narrow type if using other conditional expressionsΒ #59509

Closed as not planned
@zyoshoka

Description

@zyoshoka

πŸ”Ž Search Terms

"Inferred Type Predicates"

πŸ•— Version & Regression Information

5.6.0-dev.20240801

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240801#code/C4TwDgpgBA8lC8UDeUDWEQC4oGdgCcBLAOwHMoBfAbgCgBjAe2LygYCMArHbACjgB8oxAK4AbUQEoA2gF0EUKSnRYoAcgCGqygBpkaDNlVstFXSPEzajZsCgAzQqOAR8EACYBGeey4A6B04uPDgIAHy4UACEiOaSVFAA9AmwsvRMLAHOrm4ATN6cOP6OWcFhEdFCYqJQAGQ1uL7KCIgaqhLxSVB8UIKx0jJpNvbFLu4AzPl+mUEh8OEhFX1Fgfilcw1N8C2a7YnJMKlAA

πŸ’» Code

type O = { key: string };
const objs: (O | null)[] = [{ key: 'a' }, { key: 'b' }, null];
const filtered1 = objs.filter(s => s != null); // O[]
const filtered2 = objs.filter(s => s != null && s.key == 'a'); // (O | null)[]
const filtered3 = objs.filter(s => s != null).filter(s => s.key == 'a'); // O[]

πŸ™ Actual behavior

The type inferred for filtered2 is (O | null)[] instead of O[].

πŸ™‚ Expected behavior

I would expect filtered2 to have the inferred type O[], similar to filtered1 and filtered3.

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions