3

I had some issues building my TypeScript project because the type definitions of a library had errors. The solution was to add the strictNullChecks flag.

That's really counter-intuitive to me. How does adding restrictions remove errors? Usually, it's the other way around.

The error that I had can be found on this GitHub issue.
Also, as this user pointed out:

it doesn't make sense that a strict project cannot build in a non strict one

I tried to understand their typings, but they are using advanced TypeScript features with deep generic types and they didn't documented their code.

Yasin Br
  • 1,675
  • 1
  • 6
  • 16
Cl00e9ment
  • 773
  • 1
  • 13
  • 30
  • Could you provide a self-contained [mre] of the phenomenon you're describing, preferably one in plain text that could be pasted into an IDE of the reader's choice and where turning on `--strictNullChecks` removes errors? Or are you asking us to give *you* such an example here? – jcalz Jun 03 '22 at 19:58
  • I mean, I could contrive an example like this: [error without strictNullChecks](https://tsplay.dev/N9E01N) yet [fine with strictNullChecks](https://tsplay.dev/mMMQ1m). If I wrote up why that happens, would you consider it an answer to your question? Or are you looking for some *practical* example of where this happens (which might be harder to come by)? – jcalz Jun 03 '22 at 20:04
  • @jcalz Your example was what I needed and I'll accept it as an answer even tho it's a bit too ad-hoc. – Cl00e9ment Jun 03 '22 at 21:43

1 Answers1

0

I think, typescript does not actually add "checks", it modifies types of null and undefined, which may causes type errors (in fact, that's what it's meant to be), as I understood. https://www.typescriptlang.org/tsconfig#strictNullChecks

When strictNullChecks is true, null and undefined have their own distinct types and you’ll get a type error if you try to use them where a concrete value is expected.

And when you turn this option on - it causes type errors in one way, and when you fix all errors and turn the option back - it will cause errors in another way.

  • The documentations says that with `strictNullChecks = false`, the types `null` and `undefined` are ignored by the language. With `strictNullChecks = true`, they have their own distinct types and it can cause type error upon compiling. Maybe you're right but I'm not really grasping the concept. A simple-to-understand practical example would be welcome. – Cl00e9ment Jun 03 '22 at 19:12