Previously, the type analysis performed for type guards was limited to if statements and ?: conditional expressions and didn’t include effects of assignments and control flow constructs such as return and break statements. TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters. In practical terms, strict null checking mode requires that all files in a compilation are null- and undefined-aware. Thus, declaration files that are updated to use null- and undefined-aware types can still be used in regular type checking mode for backwards compatibility. In particular, the null and undefined types are automatically erased from union types in regular type checking mode (because they are subtypes of all other types), and the ! non-null assertion expression operator is permitted but has no effect in regular type checking mode. The new features are designed such that they can be used in both strict null checking mode and regular type checking mode. The same is true for the relationship of T to T | null. So, whereas T and T | undefined are considered synonymous in regular type checking mode (because undefined is considered a subtype of any T), they are different types in strict type checking mode, and only T | undefined permits undefined values. 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). StrictNullChecks switches to a new strict null checking mode. The type checker previously considered null and undefined assignable to anything.Įffectively, null and undefined were valid values of every type and it wasn’t possible to specifically exclude them (and therefore not possible to detect erroneous use of them). Previously it was not possible to explicitly name these types, but null and undefined may now be used as type names regardless of type checking mode. TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively.
0 Comments
Leave a Reply. |