Bug Report
Type narrowing using the typeof operator seems to act differently if an early parenthesis grouping is used.
🔎 Search Terms
type narrowing typeof parentheses
🕗 Version & Regression Information
4.1.3
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type narrowing
⏯ Playground Link
Playground link with relevant code
💻 Code
declare const foo: string
if (typeof foo === 'string') {
console.log(foo)
} else {
// @ts-expect-error
console.log(foo.toString()) // errors, 'foo' is type 'never'
}
if ((typeof foo) === 'string') {
console.log(foo)
} else {
// @ts-expect-error
console.log(foo.toString()) // doesn't error, 'foo' is still type 'string'
}
🙁 Actual behavior
In the second case, foo isn't narrowed to type never.
🙂 Expected behavior
In the second case, foo is narrowed to type never.
Bug Report
Type narrowing using the typeof operator seems to act differently if an early parenthesis grouping is used.
🔎 Search Terms
type narrowing typeof parentheses
🕗 Version & Regression Information
4.1.3
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
In the second case,
fooisn't narrowed to typenever.🙂 Expected behavior
In the second case,
foois narrowed to typenever.