# 基础类型(Basic Types)
TypeScript 支持 JavaScript 的数据单元,并且增加了一些方便实用的其他数据单元,比如枚举
- Boolean(布尔值)
- Number(数值)
- String(字符串)
- Array(数组)
- Any(任意类型)
- Void
- Null
- Undefined
- Never
- Object(对象)
let flag: boolean = false;
let num: number = 1;
let str: string = 'abc';
let arr: number[] = [1, 2, 3];
let any: any = 1;
function func(): void {
console.log('hello world');
}
let u: undefined = undefined;
let n: null = null;
function func(): never {
throw new Error('error');
}
declare function create(obj: object | null): void;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
- Tuple(元组)
let arr: [string, number];
arr = [1, 'abc']; // Error
arr = ['abc', 1]; // Ok
1
2
3
2
3
- Enum (枚举)
默认情况,元素的编号从0
开始
enum Animal {Cat, Dog, Fox};
let animal: Animal = Animal.Cat;
1
2
2
# 类型断言(Type Assetions)
显示指定变量的类型,有<>
和as
两种写法,目前在.tsx
文件中只支持as
写法的类型断言
let str: any = 'hello world';
let len1: number = (<string>str).length;
let len2: number = (str as number).length;
1
2
3
2
3