How adding more restrictions to the TypeScript type system can solve some errors?

Viewed 36

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.

1 Answers

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.

Related