Walt W Walt W - 1 month ago 7
TypeScript Question

Typescript strictNullChecks with limited scope

I'm using the Ionic v2 beta with Typescript, and would love to use

--strictNullChecks
. However, dependencies of my project, such as @angular/core, cause compilation errors when I put
strictNullChecks: true
in my
tsconfig.json
.

Is there a way to declare a directory / file / module / class as abiding by strictNullChecks, even when the overall project cannot be compiled with the flag?

It seems that, since
--strictNullChecks
is not the default, it will have extremely limited use (despite being an extremely useful feature) as a result of dependencies that were written without the flag.

Answer

If your dependencies are all in .js and .d.ts files (that is, not ts source) then you can use a flag and tell the compiler to skip checking your libs, which will result in no errors from the libs.

There are two new flags for that:

skipLibCheck:

Don’t check a the default library (lib.d.ts) file’s validity

skipDefaultLibCheck:

Don’t check a user-defined default library (*.d.ts) file’s validity

And a bit more about it in the What's new in Typescript 2:

TypeScript 2.0 adds a new --skipLibCheck compiler option that causes type checking of declaration files (files with extension .d.ts) to be skipped. When a program includes large declaration files, the compiler spends a lot of time type checking declarations that are already known to not contain errors, and compile times may be significantly shortened by skipping declaration file type checks.

Since declarations in one file can affect type checking in other files, some errors may not be detected when --skipLibCheck is specified. For example, if a non-declaration file augments a type declared in a declaration file, errors may result that are only reported when the declaration file is checked. However, in practice such situations are rare.

Comments