TypeScript

类型断言

类型断言可以指定一个值的类型,可以使用<类型>值值 as 类型语法

let someValue: any = "this is a string";

// 尖括号 语法
let strLength: number = (<string>someValue).length;
// as 语法
let strLength: number = (someValue as string).length;

非空断言

在变量后使用!修饰,标识断言该变量不为空

let mayNullOrUndefinedOrString: null | undefined | string;
mayNullOrUndefinedOrString!.toString(); // ok
mayNullOrUndefinedOrString.toString(); // ts(2531)

TODO:Decorators

https://blog.logrocket.com/a-practical-guide-to-typescript-decorators/

results matching ""

    No results matching ""