Skip to content

false ? ... behavior is weird. #59731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sparecycles opened this issue Aug 23, 2024 · 2 comments
Closed

false ? ... behavior is weird. #59731

sparecycles opened this issue Aug 23, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@sparecycles
Copy link
Contributor

sparecycles commented Aug 23, 2024

πŸ”Ž 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.

@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 23, 2024

Either disable typechecking in unreachable code or still apply narrowing

#26914

And #39995 (for why line 7 doesn't behave the same way).

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 26, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants