How do Typescript 2 apply non-empty types?

I know that the general explanation is that the compiler performs static type checking, but what methods does the compiler specifically use to verify that unimaginable types accidentally come into play?

+4
source share
2 answers

Uncertainty / Uncertainty Checking is simply changing the type of types from type models. As usual, all this happens during the type-checking phase, and nothing is in the emitted JavaScript to ensure this at runtime.

In any case, you can think of the type as a domain of values. For example, a domain booleanusually represents only two values trueand false. string- an unlimited domain that contains strings of the type "hello"and "world"and any other string.

TypeScript verifies that when a value is used at some type position, that value is in a domain of that type. For example, trueit is not in the domain number, so it is unacceptable to try to use truewhere it is expected number.

TypeScript , null undefined . , boolean : true, false, undefined null. , , undefined null , true false. , substr string null undefined, .

TypeScript , null undefined . null , string, string. string | null , string null.

Build 2016, 44:30, , , , , , , .

+6

TypeScript ; , JavaScript. - :

let foo: { myProperty: string } = { myProperty: "g" }; // myProperty is non-nullable

foo["myProperty"] = undefined; // bypass type checker!
0

Source: https://habr.com/ru/post/1655555/


All Articles