I know a general explanation is that the compiler does static type checking, but what methods specifically does the compiler use to verify that no nullable types accidentally come into play?
null means no value. To make a variable null we must assign null value to it as by default in typescript unassigned values are termed undefined. We can use typeof or '==' or '===' to check if a variable is null or undefined in typescript.
Non-null assertion operator syntaxThe non-null assertion operator is an exclamation mark ( ! ), and this is placed after the variable or expression that we want to tell TypeScript isn't null or undefined .
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.
Use the Record utility type to enforce the type of an object's values in TypeScript, e.g. type Animal = Record<string, string> . The Record utility type constructs an object type, whose keys and values are of specific type. Copied!
The nullability/undefinedness checking is just a change in how the type system models types. As usual, this all happens during the typechecking phase and nothing is in the emitted JavaScript to enforce this at runtime.
Anyway, you can think of a type as a domain of values. For example, the domain of boolean
is usually just the two values true
and false
. string
is an unbounded domain that contain strings like "hello"
and "world"
and every other string.
TypeScript checks that when a value is used in some type position, that value is in the domain of that type. For example, true
is not in number
's domain, so it's illegal to try to use true
where a number
is expected.
In TypeScript without strict null checking, null
and undefined
are in the domain of every type. So the type boolean
actually has four values: true
, false
, undefined
, and null
. As people usually discover, this is bad, because undefined
and null
don't behave exactly like true
and false
. For things with properties and methods, it's worse, because substr
exists as a method of every value in the domain of string
except null
and undefined
, so it's really annoying.
In TypeScript with strict null checking, null
and undefined
move out of the domain of every type and into their own types. Now the value null
cannot be used in a place where a string
is expected, because it's no longer in string
's domain. The type string | null
now represents a value which might be some member of the string
domain or the special value null
.
This explanation is adapted from Anders Hejlsberg's talk from Build 2016 starting around 44:30 and as you might imagine, he does a better job explaining it than I do, especially because he has some nice slides to accompany it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With