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.

disallow comparing to null and undefined unless they are valid cases in strict null mode

See original GitHub issue

the rationale is the same as for --noUnusedLocals which in this case would be --noUselessNullChecks (i am not proposing a new flag, the existing --strictNullChecks flag should be used, this is for illustration only)

declare const value: number;
if (value !== null) { // <-- somehow valid, expected to be invalid, since number doesn't have null as a possible value
}
if (value !== '') { // <-- invalid as expected 
}

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:29
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
c-vettercommented, Jun 28, 2021

The rationale behind the current behavior is understandable. However, in my view it lends itself to sloppy typing. My strategy is to guard the boundaries where data comes in from unsafe (=non-TS) sources. And there, null and undefined need to be accounted for, but that is really no special case as far as typing is concerned. Rather, null and undefined are just necessarily possible value types. Based on that, strict typing makes a lot of sense.

Until today, I fully expected the behavior this issue calls for to be present when strict is active. Then I came across a case similar to the OP sample and wondered why there was no error message. I even checked the tsconfig to see if somebody had disabled strict or strictNullChecks.

Please add this behavior, possibly behind a new setting 🙂

@DanielRosenwasser

Internal implementation detail: right now we use the comparability relationship for type assertions (casting) - if we did this, we’d need to have a separate type relationship (or ad-hoc check) for allowing you to cast undefined/null to any other type. Is this still a blocker? If yes: How is this casting required? Isn’t the problem an unintended non-distinction, which would be functionally equivalent to casting?

@RyanCavanaugh This issue is marked Awaiting More Feedback. Can you say roughly what this issue would need to get some traction, how far off it is?

4reactions
Spongmancommented, Dec 9, 2016

i would suggest to make type safety the default, and have interop require explicit casting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

strictNullChecks - TSConfig Option - TypeScript
When strictNullChecks is false , null and undefined are effectively ignored by the language. This can lead to unexpected errors at runtime.
Read more >
Typescript strictNullChecks with limited scope - Stack Overflow
When you run tsc from inside directory a , you'll get strict null checks for a/code.ts . This approach isn't without drawbacks: If...
Read more >
Strict null checking the Visual Studio Code codebase
To do this, we created a new TypeScript project file called tsconfig.strictNullChecks.json that enabled strict null checking and initially ...
Read more >
How to enforce strict null checks in TypeScript - GeeksforGeeks
In Typescript to enforce strict null checks in tsconfig.json file, we need to enable “strictNullChecks” to true. When “strictNullChecks” is ...
Read more >
Null vs. Undefined - TypeScript Deep Dive - Gitbook
Remember how I said you should use == null ? Of course you do (cause I just said it ^). Don't use it...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found