type guard
For union types, type narrowing is required. If or Typeof is needed to narrow the type and guard the type.
typeof
The typeof operator can obtain the base types of values, such as string, number, Bigint, Boolean, symbol, undefined, object, and function. However, there is no NULL.
TS verifies the return value of TypeoFD again, which is also a Type Guard.
As follows, although typeof is used to narrow the type, JavaScritp null is an object, and TypeScript also raises an error.
// @errors: 2531
function printAll(strs: string | string[] | null) {
if (typeof strs === "object") {
for (const s of strs) {
//Object is possibly 'null'.Object is possibly 'null'.
console.log(s);
}
}
else if (typeof strs === "string") {
console.log(strs);
} else {
// do nothing
}}
Copy the code
TS is the STRS type narrowing a string [] | null.
Truthiness narrowing
TypeScript only helps us with type checking, and if we don’t pay attention to variable values when type narrowing, we can hide bugs when JS is running.
function printAll(strs: string | string[] | null) { // !!!!!!!!!!!!!!!! // DON'T DO THIS! // KEEP READING // !!!!!!!!!!!!!!!! if (strs) { if (typeof strs === "object") { for (const s of strs) { console.log(s); } } else if (typeof strs === "string") { console.log(strs); }}}Copy the code
The in operator
type Fish = { swim: () => void }; type Bird = { fly: () => void }; type Human = { swim? : () => void, fly? : () => void }; function move(animal: Fish | Bird | Human) { if ("swim" in animal) { // animal (parameter) animal: Fish | Human } else { // animal (parameter) animal: Bird | Human } }Copy the code
The instanceof operator
Type narrowing is also done
The assignment
slightly
Control flow analysis
This analysis of code based on reachability is called control flow analysis. TS will be based on this, that is, TS will analyze the variables in the different branch logic, thus narrowing the type.
Types of prediction
ParameterName is Type, and parameterName must be the name of the parameter in the function signature.
function isFish(pet: Fish | Bird): pet is Fish { return (pet as Fish).swim ! == undefined; }Copy the code
TODO
For class, you can use this is Type to narrow the Type
www.typescriptlang.org/docs/handbo…
Discriminated Unions -> Discriminated Unions
```TypeScript``` interface Shape { kind: "circle" | "square"; radius? : number; sideLength? : number; }// we can narrow kind with ===, but we can use! Function getArea(shape: shape) {if (shape.kind === "circle") {return math.pi * shape.radius! * * 2; }} interface Circle {kind: "Circle "; radius: number; } interface Square { kind: "square"; sideLength: number; }type Shape = Circle | Square; Function getArea(shape: shape) {if (shape.kind === "circle") {return math.pi * shape.radius ** 2; }}Copy the code
Never type
When the type is narrowed, you can lower the options for the union type. When it Narrows to a point, all possibilities have been eliminated and there should be no type of surplus. In TS, never represents a state that should not exist.
The never type can be assigned to any type, but only the never type can be assigned to never.
Exhaustiveness checking
The never type ensures that all types are exhausted after code union types change. Prevent bug.
See: www.zhihu.com/question/35…