question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Narrowing types by `typeof` inside `switch (true)` does not work

See original GitHub issue

TypeScript Version: 3.8.3 Nightly

Search Terms:

  • switch statement
  • switch (true)

Code

let x: string | undefined;

if (typeof x !== 'undefined') {
  x.trim(); // works as expected
}

switch (true) {
  case typeof x !== 'undefined':
    x.trim(); // thinks x is potentially undefined
}

Expected behavior:

Narrowing types should work inside the case.

Actual behavior:

Any assertion in case seems to be disregarded

Playground Link: https://www.typescriptlang.org/play/index.html?ts=Nightly#code/DYUwLgBAHgXBDOYBOBLAdgcwgHwgVzQBMQAzdEQgbgChqUSIAKMATwAcQB7BqCAQgC8AiAHICxMmgoiAlBADe1CNAB0yFAFtGMyhAD0eiAHdOSANbwIAQ0sgoHAMZgK1AL614RlGAcALJsh4IHKKyg42IBCsHNzQ-EKi4qTkhCIwSsqq6lo6+oZgvugWcSiWbJzOaGAoVsDALPhEyVKEbkA

Related Issues:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:32
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

24reactions
joschacommented, Mar 4, 2020

Two come to mind: • readability for big amounts of cases • being able to use fall-throughs for overlapping cases

Having said that I understand it is an edge case. I came across this pattern in a bigger codebase and wanted to make a change. That’s when I noticed that the types are not narrowed in each case.

13reactions
polomskycommented, Apr 15, 2021

I have similar usecase:

switch (true) {
    case error instanceof CustomError1:
        return error.foo;
    case error instanceof CustomError2:
        return error.bar;
    case error instanceof Error:
        return String(error);
}

And I am using this syntax in javascript because it looks better than chain of if else. Sadly can not use in typescript

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript 3.8.3 not narrowing union type in switch statement
I notice that from TS 4.3.5 and on, the type is narrowed the way I'd expect (the error disappears), but I can't change...
Read more >
Documentation - Narrowing - TypeScript
If we'd assigned a boolean to x , we'd have seen an error since that wasn't part of the declared type. x =...
Read more >
Narrowing Types in TypeScript - Formidable Labs
Type narrowing is just what it sounds like—narrowing down a general type into something more precise. Here's how to do it.
Read more >
Type-Safe TypeScript with Type Narrowing - Rainer Hahnekamp
The type of input in calcAge can be of three different types. ... Strictly speaking function is not a real type, it is...
Read more >
Type guards and assertion functions • Tackling TypeScript
Inside the body of getScore() , we don't know if the type of value number or string . ... Narrowing also works if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found