Skip to content
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

Parentheses in typeof expression break narrowing #60784

Closed
crisbeto opened this issue Dec 17, 2024 · 2 comments
Closed

Parentheses in typeof expression break narrowing #60784

crisbeto opened this issue Dec 17, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@crisbeto
Copy link

πŸ”Ž Search Terms

  • type narrowing
  • typeof expression

πŸ•— Version & Regression Information

  • This changed between versions 3.3 and 5.7
  • This changed in commit or PR: unclear

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.2#code/DYUwLgBAbghsCuIIF4IEYBMBmCMDOEeYATgJYB2A5hAD4TnwC2ARiMQNwBQnA9DxAHUA9sQDWeTqQBmEABRgAngAcQQmbARJk2iAHIGLNroCUEAN6cIEAMZDyeIaAB0wIZVkbEEAFQQMxrgBfbj4IABEhEAJyIUgAdxFRSRlZeWVVdThEU21UfSZWYhNzSxs7B2dXd08kX38gkP4IqPpYiASxZLlFFTVoLK0dWXzDIuNTCytbe0cQFzcPAZ8-AM5g3ibI6LaOpOk5NN7MzRyhkcKTCdLpirmqxc1l+rWgA

πŸ’» Code

let value = 123 as string | number;

// Works
if (typeof value === 'number') {
  console.log(value * 2);
}

// Does not work
if ((typeof value) === 'number') {
  console.log(value * 2);
}

// Does not work
if (typeof value === ('number')) {
  console.log(value * 2);
}

// Does not work
if ((typeof value) === ('number')) {
  console.log(value * 2);
}

πŸ™ Actual behavior

As soon as parentheses are added around the typeof expression or the value it's being compared against, narrowing no longer works.

πŸ™‚ Expected behavior

Narrowing shouldn't be affected by parentheses.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #42203.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 17, 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 Dec 20, 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