How do I set up my tsconfig with "strictNullChecks": true?


#1

I added the “strictNullChecks”: true flag to my tsconfig:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictNullChecks": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "src/**/*.spec.ts",
    "src/**/__tests__/*.ts"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

Then I tried doing something in my ts file that should cause a compiler error, but there are no errors:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

function trimAndLower(text) {
  return text.trim().toLowerCase();
}
let text;
text = 'some string';
text = null;


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController) {

  }
}

I managed to get strictNullChecks working in a simple project with just one ts file and a tsconfig, but tried moving over to try in the Ionic framework and can’t get it to work. It’s just the basic started app (ionic start myApp blank), and I added nothing else other than what I’m showing above.


#2

Just realized that I’m an idiot and I didn’t actually declare the type.