数据类型
- number
- string
- boolean
- array ```javascript // 声明数组方式1 let arr: (number | string | boolean)[] = [1, 2, 3, ‘4’, true] console.log(arr);
// 声明数组方式2 - 范型
type objInfo = {name: string, age: number}
let arr2: Array
5. tuple
6. 联合类型: union
7. 字面量类型:literal
8. 枚举类型:enum
```javascript
enum Role {
Reporter = 1,
Developer = 3,
Maintainer = 10,
Owner = 12,
Guest = 110
}
console.log(Role.Developer) // 3
console.log(Role[110]) // "Guest"
- any
TS + any = JS
- unknow
保证类型的安全,相对比any好些
- void
函数永远没有返回值
function say(){
console.log('lala');
}
- undefined
- never
一个函数永远无法执行完成
function throwError(msg, code){
throw{
msg,
code
}
}
throwError('找不到了', 404)
- 类型适配(类型断言) Type Assertions
人为告诉计算机类型
let message: any;
message = '123'
message.endWith('c')
// 声明方式1
let ddd = (<string>message).endWith('c')
// 声明方式2
let ddd = (message as string).endsWith('3')