-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Closed
Copy link
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
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
.
JeanMeche, jase88, crisbeto and kisfairmed
Metadata
Metadata
Assignees
Labels
Experience EnhancementNoncontroversial enhancementsNoncontroversial enhancementsSuggestionAn idea for TypeScriptAn idea for TypeScript