8000 typeof type narrowing acts differently with equivalent parentheses grouping · Issue #42203 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
typeof type narrowing acts differently with equivalent parentheses grouping #42203
@ferdaber

Description

@ferdaber

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0