Skip to content

false ? ... behavior is weird.Β #59731

Closed as not planned
Closed as not planned
@sparecycles

Description

@sparecycles

πŸ”Ž Search Terms

ternary false inference

πŸ•— Version & Regression Information

Looks like this has been the case forever. (since oldest playground version 3.3333Μ…)

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABMAFAfQFyIM5QE4xgDmAlIgN4C+A3AFAA2AplIgB5a4HGIA+i4AE0bBCjAXQgJciAI5YARnDhMAhkgC8ifCEYSpLGRESbgK+tkaIV2RJLC46tGYgD8bV8hSsyWMCHr0iL7+9HSGHqweqN5BiH4BsfGhtKbmlm6RbtE+cSGJIdRAA

πŸ’» Code

function f(_: string) {};
let x: string | undefined;
const q: boolean = true;
const qc = false as const;

q ? x ? f(x) : null : null;
qc ? x ? f(x) : null : null; // no error as below despite qc being `const false` so it should behave the same?
false ? x ? f(x) : null : null; // error on `x` being string | undefined in impossible branch after x is checked.

πŸ™ Actual behavior

image

πŸ™‚ Expected behavior

Either disable typechecking in unreachable code or still apply narrowing (as is done in the qc ? ... line)?

Additional information about the issue

Just sharing since it surprised me a little.

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