Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I am not getting warnings about StrictNullChecks in typescript

I have the following code in typescript:

interface Member {
    name: string,
    age?: number
}

class Person implements Member {
    name: string;
    constructor(name: string ){
        this.name=name;
    }
}

function bar(person: Member) {
    return "Hello, " + person.name + " " + person.age;
}

let person = new Person("John");
console.log(bar(person));

I am supposed to get Object is possibly 'undefined' warning inside bar function when i declare person.age since not every Member can have age.

my typescript config looks like this:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "strictNullChecks": true,
    "outDir": "./built"
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

Any idea why this is not working for me? I am using WebStorm Editor!

like image 703
Mizlul Avatar asked Dec 12 '18 10:12

Mizlul


People also ask

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.

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. This flag applies for all components that are part of the application.

How do you do a null check in TypeScript?

To check for null in TypeScript, use a comparison to check if the value is equal or is not equal to null , e.g. if (myValue === null) {} or if (myValue !== null) {} . If the condition is met, the if block will run. Copied!


1 Answers

Turn on "strict": true, in the tsconfig.json to enable this warning.

Or if you don't want all the strict options you in need:

"strictNullChecks": true,
"strictPropertyInitialization": true,

See the documentation for more information

--strictNullCheck:

In strict null checking mode, the null and undefined values are not in the domain of every type and are only assignable to themselves and any (the one exception being that undefined is also assignable to void).

--strictPropertyInitialization

Ensure non-undefined class properties are initialized in the constructor. This option requires --strictNullChecks be enabled in order to take effect.

The second one is the one you want (but needs strictNullChecks to work)

Btw, as @jayasai amerineni mentions, your example shouldn't trigger this warning.

like image 185
Philipp Avatar answered Sep 22 '22 23:09

Philipp