typescript笔记

date: 2019.07.06; modification:2019.07.11

目录:

1 类型声明

类型 声明关键字
字符串 string
布尔 boolean
数值 number
数组 number[] 或 Array<number> 或 any[]
枚举 enum Role {Employee = 3, Manager, Admin}
函数返回值类型 function add(x: it): string { return "Hello"; }
可选参数 function buildName(firstName: string, lastname?: string) {}
默认参数 function buildName2(firstName = 'Mike') {}
类成员 class Person{ name:string; // 定义this.name类型 ... }
getter/setter class Hello{ private thename: string; get name(): string { return this.thename; }
接口 interface LabelValue{ label: string; }
可选属性 interface Person{ name:string; age?:number; }

多选类型: let a:string|number;

2 ts + react + webpack

yarn add -D @types/react @types/react-dom awesome-typescript-loader source-map-loader

2.1 tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist/",
            "sourceMap": true,
            "noImplicitAny": true,
            "module": "commonjs",
            "target": "es6",
            "jsx": "react"
    },
    "include": [
        "./src/**/*"
    ]
}