Skip to content

Parentheses in typeof expression break narrowingΒ #60784

Closed as not planned
Closed as not planned
@crisbeto

Description

@crisbeto

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

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