ES数据类型 | TS数据类型 | ||
---|---|---|---|
Number | 数字 | number | 数字 |
String | 字符串 | string | 字符串 |
Boolean | 布尔 | boolean | 布尔 |
Symbol | 符号 | symbol | 符号 |
BigInt | 高精度整数类型 | bigint | 高精度整数类型 |
Undefined | 未定义 | undefined | 未定义 |
Null | 空对象 | null | 空对象 |
Object | 对象 | object | 对象 |
Array | 数组 | ||
function | 函数 | ||
void | 空 | ||
any | 任意 | ||
unkonwn | |||
never | |||
元组 | |||
枚举 | |||
高级类型 |
ts中的数据类型,只能获取和声明时相同的类型的值,如果类型不同则会报错 “Type ‘a’ is not assignable to type ‘b’.”,意思是不能将 a 类型的值赋给 b
- number 数字类型
const num: number = 123;
- string 字符串类型
const str: string = 'hello world';
- boolean 布尔类型
const bool: boolean = true;
- symbol 符号
- bigint 高精度整数类型
- undefined 未定义
const un: undefined = undefined;
- null 空对象
const n: null = null;
这个地方要注意,{} 不是空对象,null 类型只有一个值(null),如果我们将 {} 赋值给 n,则
const n: null = {}; // Type '{}' is not assignable to type 'null'.
- object 对象
对象类型的兼容性很高,其中对象(object)、数组(Array)、函数(Function)、枚举(enum)类型的值都可以赋值给以对象类型为约束的变量
const obj1: object = {name: 'zhangsan', age: 20};
const obj2: object = ['hello', 123];
const obj3: object = function(){}
enum Direction {
Center = 1
}
let obj4: object = Direction;
对于对象来说,每一个值都有对应的键,所以我们可以对每一个单一的键值的类型进行约束
- 使用 Object 类型不止可以定义变量 obj 的类型,和可以对对象中的所有键进行类型约束
- 此处对 key 的类型约束,对象中的每个 key 不需要一一对应
- 如果类型不同一样会报错 (Type ‘a’ is not assignable to type ‘b’)
const obj: {name: string, age: number} = {name: 'zhangsan', age: 20};
const obj: {name: string, age: number} = {age: 20, name: 'zhangsan'};
const obj: {name: string, age: number} = {age: '20', name: 'zhangsan'}; // 注:这里报错的是 age 字段,在博客中不会看到飘红
- Array 数组类型
const arr: Array = [];
对于数组类型,我们可以约束其中项的类型,不过因为数组的push、pop等方法,当数组在进行一些操作之后,项的下标会发生改变,所以我们只能模糊的去约束数组中项的类型
- 当数组中所有项的类型统一且唯一时,我们可以使用(…此处查看名称 -->> string[])
- 当数组中类型不同时,我们可以使用联合类型来约束数组类型
//当数组中所有项的类型统一,我们使用纯数组声明方法
const arr1: Array<string> = ["123"];
const arr2: string[] = ["hello"];
// 当数组中类型不同时,我们使用联合类型
const arr3: Array<string | number> = [123, "string"];
- function 函数类型
const func1: Function = function () {};
const func2: Function = () => {};
对于函数类型,我们可以约束它的入参和返回值
function func(a: number, b: number): number {
return a + b;
}
func(1, 2); // 3
对入参和返回值都有一定约束
入参
- 数据类型一定要相同
- 入参的数量要与函数形参的数量相等,太多或太少都会报错
function func(a: number, b: number): number {
return a + b;
}
func('1', 2); // 入参与形参的数据类型不同
func(1, 2, 3); // 入参数量大于形参数量
func(1); // 入参数量小于形参数量
返回值
- 数据类型一定要相同
- 数据类型一定要唯一不可有其他的可能性,所以函数应是纯函数
function func(a: number, b: string): string {
if (a) {
return a;
} else {
return b;
}
}
func(20, '张三'); // 数据类型不同,且返回值类型不唯一
- any 任意类型
let temp: any = "any";
任意类型可以接收任意类型的参数,并且可以使用各种自身的方法
temp = 'hello woeld';
temp = 123;
temp = true;
temp = [];
temp = {};
temp.length; // string success
temp++; // number success
!temp; // boolean success
temp.pop(); // Array success
Object.keys(temp); // object success
- unknown 未知
未知类型可以接收任意类型的参数,但是不可以使用自身的方法
temp = 'hello woeld';
temp = 123;
temp = true;
temp = [];
temp = {};
temp.length; // string error
temp++; // number error
!temp; // boolean error
temp.pop(); // Array error
Object.keys(temp); // object error
- never
never 类型的值是永远不会发生任何更改
function error(): never {
throw new Error('error');
// ...
}
let endless = () => {
while(true){}
}
const arr: never[] = [];
- 元组
元组类型是特殊的数组,元组类型限制了数组的个数和类型
let arr: [number, boolean];
arr = [1, true];
元组类型限制了数组的个数和类型,所以若是数组中项的数量与元组中类型的数量不等,或是项所对应的类型不同都会报错
在元组类型中,项的类型要和元组中的类型一一对应
- 元组越界
可以往数组中追加值,但是不能使用
const arr: [number, boolean] = [123, true];
arr.push('hello'); // Argument of type '"hello"' is not assignable to parameter of type 'number | boolean'.
arr[2];
此时 push 操作会报错,"hello"这个值不能赋值给"number | boolean"这个类型