8

I am specifying types on a function. TypeScript types are (as of current TypeScript versions) non-nullable by default. However I don't get any errors when I run a function with null or undefined.

function sayHello(name: string){
  console.log(`Hello ${name}`)
}

In the code above sayHello(undefined) and sayHello(null) should both fail.

What they currently do in TypeScript 3.8.3:

Hello null
Hello undefined

vsCode gives no warnings:

enter image description here

Why is TypeScript not warning when setting a non-nullable value as null?

8
  • TypeScript types are not nullable by default. Commented Mar 27, 2020 at 16:10
  • Try it. sayHello(undefined) logs Hello undefined Commented Mar 27, 2020 at 16:12
  • Also @BenBotvinick I've read that TypeScript types were nullable by default. it looks like this changed at some point: see "TypeScript originally started out with the idea that types were always nullable. " at devblogs.microsoft.com/typescript/announcing-typescript-2-0 Commented Mar 27, 2020 at 16:18
  • 3
    Maybe your compiler is misconfigured. Try adding the "strict" option. Also, try throwing a number into the function because if it is accepting null and undefined it's probably accepting other incorrect types, and that would mean your compiler is definitely misconfigured. Commented Mar 27, 2020 at 16:20
  • Also, if you read one second further you would find "TypeScript originally started out with the idea that types were always nullable. This meant that something with the type number could also have a value of null or undefined. Unfortunately, this didn’t provide any protection from null/undefined issues. In TypeScript 2.0, null and undefined have their own types which allows developers to explicitly express when null/undefined values are acceptable. Now, when something can be either a number or null, you can describe it with the union type number | null." Commented Mar 27, 2020 at 16:21

2 Answers 2

9

Do you have a tsconfig.json file? If so, how does it look like?

Make sure, you have following entries in your tsconfig.json (shortened for the sake of brevity):

{
  "compilerOptions": {
   "strictNullChecks": true
  }
}

From the TypeScript doc:

strictNullChecks: switches to a new strict null checking mode.

Sign up to request clarification or add additional context in comments.

3 Comments

@mikemaccana Correct, so unless you want to activate all the strict type checking options listed there at once, you can simply go one by one as per your need. :D
In my case, typescript was not warning when optional functions were called without null check. After adding "strictNullChecks": true, it started showing the warnings. Thanks.
3

Not nullable is now the default in TypeScript. You should get the typescript compilation error:

Argument of type 'null' is not assignable to parameter of type 'string'. ts(2345)

If you don't get that, you may want to take a look at your compiler options. I would recommend the strict option.

You can set that up with a tsconfig.json file containing something like this:

{
  "compilerOptions": {
    "strict": true,
  },
}

4 Comments

Using typescript 3.8.3, running sayHello(undefined) does not warn.
What's not what I get. VSCode gives no compilation errors. ts-node runs the .ts file with no warnings or errors and prints Hello null
IMHO, ts-node is not a good tool. I recommend getting used to using tsc and running the compiled version through node. ts-node often obfuscates problems.
Thanks @klas I needed the strict option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.