Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript strictNullChecks with limited scope

Tags:

typescript

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.

like image 298
Walt W Avatar asked Oct 20 '16 20:10

Walt W


People also ask

How do you enforce a strict null check in TypeScript?

In Typescript to enforce strict null checks in tsconfig. json file, we need to enable “strictNullChecks” to true. When “strictNullChecks” is false, the language generally ignores variables of type null and undefined. If null and undefined is used in places where a definite value is expected, it raises an error.

How do I disable strict null check?

Setting the option strictNullInputTypes to false disables strict null checks within Angular templates.

How do I enable strictNullChecks TypeScript?

To enable StrictNullChecks open tsconfg. sys and add "strictNullChecks": true under the compilerOptions . With this set to true , you'll get a type error if you try to use null or undefined wherever Typescript expects a concrete type.


2 Answers

If you'd like to scope strictNullChecks to a particular directory within your project, you can do it with an extends clause in your tsconfig.json. For example, if your project looks like this:

a/
  code.ts
b/
  other.ts
tsconfig.json

And you want to enable strict null checking within a, you could add a/tsconfig.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "strictNullChecks": true
  }
}

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 a/code.ts imports ../b/other.ts, then you'll get strict null checking for b/other.ts, too.
  • If you run tsc from the root directory, you won't get strict null checking for a/code.ts.

You're effectively creating a new, strict, sub-project within your larger project. You'd want to make sure to run tsc for both projects as part of your build process. It's not perfect, but it might help you migrate a large project to strictNullChecks bit by bit.

like image 153
danvk Avatar answered Sep 21 '22 22:09

danvk


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 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.

like image 21
Nitzan Tomer Avatar answered Sep 22 '22 22:09

Nitzan Tomer