let str = "str";
// 联合类型union types
let numberOrString: number | string = "Good Morning!";
numberOrString = 123;
// 类型断言type assertion
function getLength(input: string | number): number {
const str = input as string;
if (str.length) {
return str.length;
} else {
const num = input as number;
return num.toString().length;
}
}