This is the 8th day of my participation in the Challenge. For details, see:More article challenges

Typescript document address:Type inference

Union types

Let numberOrString: // Let numberOrString: Number | the string / / when the TypeScript uncertainty which is a joint type of the variable types, we can only access the joint type of all types of Shared attributes or methods:  numberOrString.length numberOrString.toString()Copy the code

Type assertion – type assertions

function getLength(input: string | number): Number {const STR = input as string if (STR. Length) {return STR. Length} else {const number = Return number.toString().length}} Return number.tostring ().length}}Copy the code

Type Guard – type guard

function getLength2(input: string | number): Number {if (typeof input === 'string') {// Type guard return input.length // string} else {return Input.tostring ().length // number type}}Copy the code

Reference documentation: Typescript Chinese

People are lazy, do not want to picture, are their own blog content (dry goods), hope to help everyone

Public number: xiao He growth, The Buddha department more text, are their own once stepped on the pit or is learned

Interested partners welcome to pay attention to me, I am: He young life. Everybody progress duck together