Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Typescript 2 enforce non-nullable types?

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?

like image 708
Shou Avatar asked Sep 23 '16 00:09

Shou


People also ask

How do you handle undefined and null in TypeScript?

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.

How do you assert not null 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 .

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.

Does TypeScript enforce types?

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!


1 Answers

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.

like image 168
Ryan Cavanaugh Avatar answered Sep 22 '22 19:09

Ryan Cavanaugh