@Secretmm
2021-01-20T07:35:20.000000Z
字数 877
阅读 663
梳理
定义变量类型的冒号前没有空格,冒号后有空格;表示可选属性/参数的问号前后没有空格
let foo: string;let person: {name: string;age?: number;};
interface和对象中属性类型定义后用分号interface和对象中属性类型定义后用分号, 而不是逗号;拖尾分号规则与拖尾逗号一致
interface Peaple {name: string;age: number;}type Foo = {id: string;state: number;}let bar: { status: string } = { status: 'error' };
interface和自定义类型命名使用大驼峰
interface ActiveItem {id: string;remark: string;}type DisabledItem = {id: string;remark: string;}
enum及枚举值的命名使用大驼峰
enum RequestStaus {Ready,LoadSuccess,LoadFail}
有返回值的函数在定义时要声明返回值类型;没有返回值的函数也建议在定义时声明void类型
function add(a: number, b: number): number {return a + b;}
any类型如无必要,不使用any类型;尽量将类型定义清晰
Class公共成员加publicClass中,可被外部访问的成员变量和方法(不包括constructor、get和set)在声明时显式的加public关键字
class Client {public id: number;constructor() {}public connect(url: string) {// TODO}}
as在类型断言或者类型强制转换时,使用as关键字,而不是<>
interface Foo {bar: number;bas: string;}// 推荐const foo = {} as Foo;// 不推荐const foo = <Foo>{};
