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.

Should warn against invalid typeof expression

See original GitHub issue

TypeScript Version: 3.2.2

Search Terms: typeof undefined


// This condition will always return 'false' since the types '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' and '42' have no overlap.
typeof 42 === 42

typeof 42 === undefined

typeof 42 === null

Expected behavior:

typeof 42 === undefined

typeof 42 === null

Should warn as what typeof 42 === 42 warns.

Actual behavior:

No warnings

Playground Link: This condition will always return ‘false’ since the types ‘“string” | “number” | “bigint” | “boolean” | “symbol” | “undefined” | “object” | “function”’ and ‘42’ have no overlap. typeof 42 %3D%3D%3D 42 typeof 42 %3D%3D%3D undefined typeof 42 %3D%3D%3D null

Related Issues:

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

ajafffcommented, Dec 30, 2018

@fatcerberus TypeScript always allows comparing with null or undefined (because reasons). There’s nothing special about comparing the result of a typeof expression. That’s what @jack-williams means by:

would require special casing the check (with typeof and null), rather than changing the comparability relation.

IMO comparing something with null or undefined shouldn’t be treated different from any other value. If you need to, your type declarations are probably wrong. But that’s already discussed in another issue and was declined for compatibility reasons. Therefore I’ve written a lint rule, see below.

At that point I wonder if this would be better addressed with a lint rule instead.

This check already exists as lint rule.

Jack-Workscommented, Mar 2, 2021

ah please fix this, I shot on my foot again today

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid typeof value '{a}' - JSLint Error Explanations
The "Invalid typeof value '{a}'" error is thrown when JSHint encounters a comparison with a typeof expression on one side and an invalid...
Read more >
why is "typeof v === 'string'" considered invalid by eslint
It's because you're using Angular, which has a built-in isString method, and you have the Angular plugin installed for eslint.
Read more >
enforce comparing typeof expressions against valid strings (valid ...
This rule enforces comparing typeof expressions to valid string literals. Options. This rule has an object option: "requireStringLiterals": true requires typeof ...
Read more >
C# Compiler warning waves | Microsoft Learn
Warning wave 5. The is and as expressions always return false for a static type because you can't create instances of a static...
Read more >
Documentation - Narrowing - TypeScript
Argument of type 'string | number' is not assignable to parameter of type 'number' ... In JavaScript, we can use any expression in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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 Post

No results found

github_iconTop Related Hashnode Post

No results found