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

typeof type narrowing acts differently with equivalent parentheses grouping #42203

Closed
ferdaber opened this issue Jan 4, 2021 · 6 comments · Fixed by #60928
Closed

typeof type narrowing acts differently with equivalent parentheses grouping #42203

ferdaber opened this issue Jan 4, 2021 · 6 comments · Fixed by #60928
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@ferdaber
Copy link

ferdaber commented Jan 4, 2021

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.

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Jan 4, 2021
@RyanCavanaugh
Copy link
Member

Narrowings only occur on predefined syntactic patterns, and this isn't one of them. I could see wanting to add parens here for clarity, though -- we should detect this one too.

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 4, 2021
softwareCobbler added a commit to softwareCobbler/TypeScript that referenced this issue Jul 15, 2021
softwareCobbler added a commit to softwareCobbler/TypeScript that referenced this issue Jul 15, 2021
@softwareCobbler
Copy link
Contributor

(can I delete the above referenced commits from this thread? I put an incorrect issue reference in the commit message and it was destined for another; I do not want to pollute the repo / resolved issue list)

@RyanCavanaugh
Copy link
Member

I don't know a way to do that. I wouldn't worry about it.

@kisfairmed
Copy link

Any chance of this to be ever fixed? It causes bigger problems in angular/angular#59215

@RyanCavanaugh
Copy link
Member

@Andarist seems like something you might be interested to do?

@Andarist
Copy link
Contributor

Andarist commented Jan 6, 2025

Expect a PR on your desk by tomorrow morning 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants