Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.基础类型 #5

Open
Zijue opened this issue Nov 12, 2020 · 0 comments
Open

2.基础类型 #5

Zijue opened this issue Nov 12, 2020 · 0 comments

Comments

@Zijue
Copy link
Owner

Zijue commented Nov 12, 2020

TS 中冒号后面的都为类型标识

JavaScript 数据类型分类

  • 原始数据类型:stringnumberbigintbooleannullundefinedsymbol
  • 非原始数据类型:object

布尔值、数字、字符串类型

let bool:boolean = true;
let num:number = 11;
let str:string = 'hello world';

数组类型

声明数组中元素的数据类型,只允许数组存放声明的类型

let arr1:number[] = [1,2,3];
let arr2:string[] = ['1','2','3'];
let arr3:(number|string)[] = [1,'2',3];

元组类型

限制长度个数,类型一一对应

let tuple:[string,number,boolean] = ['zijue',10,true];

元组在初始化赋值之后可以增加数据(只能增加元组中存放的类型),这跟 python 中元组不可变不太一样

let tuple:[string,number,boolean] = ['zijue', 10, true];
tuple.push('xiaochi');  // 添加的元素不能使用索引下标取值

枚举类型

枚举使用 enum 关键字来定义

enum USER_ROLE {
    USER,
    ADMIN,
    MANAGER
}

枚举成员会被赋值为从 0 开始递增的数字,同时也会对枚举值到枚举名进行反向映射

enum USER_ROLE { USER, ADMIN, MANAGER }

console.log(USER_ROLE['USER'] === 0);  // >> true
console.log(USER_ROLE['ADMIN'] === 1);  // >> true
console.log(USER_ROLE['MANAGER'] === 2);  // >> true

console.log(USER_ROLE[0] === 'USER');  // >> true
console.log(USER_ROLE[1] === 'ADMIN');  // >> true
console.log(USER_ROLE[2] === 'MANAGER');  // >> true

事实上,上面的例子会被编译为

var USER_ROLE;
(function (USER_ROLE) {
    USER_ROLE[USER_ROLE["USER"] = 0] = "USER";
    USER_ROLE[USER_ROLE["ADMIN"] = 1] = "ADMIN";
    USER_ROLE[USER_ROLE["MANAGER"] = 2] = "MANAGER";
})(USER_ROLE || (USER_ROLE = {}));
  • 异构枚举

异构枚举的成员值是数字和字符串的混合

enum USER_ROLE {
    USER = 'user',
    ADMIN = 1,
    MANAGER,
}
  • 常量枚举

使用 const enum 定义的枚举类型。与普通枚举的区别是,它会在编译阶段被删除

const enum USER_ROLE {
    USER,
    ADMIN,
    MANAGER,
}
console.log(USER_ROLE.USER);  // 被编译为:console.log(0 /* USER */);

any 类型

不进行类型检查

let arr:any = ['zijue',true,{name:'xiaochi'}]

nullundefined

任何类型的子类型,但是在严格模式下,不能将 nullundefined 赋给其他类型变量

let name:number | boolean;
name = null;

void 类型

只能接受 nullundefined。一般用于函数的返回值

function alertName(): void {
    alert('My name is Tom');
}

let unusable: void = undefined;

严格模式下,不能将 null 赋给 void 类型

never 类型

任何类型的子类型,never 表示不存在的值,不能把其他类型赋值给 never 类型
出现的情况有三种:

  • 错误
  • 死循环
  • 类型判断时会出现 never

symbol 类型

symbol 表示独一无二

const s1 = Symbol('key');
const s2 = Symbol('key');
console.log(s1 == s2);  // >> false

bigint 类型

numberbigint 不兼容

const num1 = Number.MAX_SAFE_INTEGER + 1;
const num2 = Number.MAX_SAFE_INTEGER + 2;
console.log(num1 == num2);  // >> true

let max: bigint = BigInt(Number.MAX_SAFE_INTEGER);
console.log(max + BigInt(1) === max + BigInt(2));  // >> false

object 类型

object 表示非原始数据类型

let create = (obj:object):void=>{}
create({});
create([]);
create(function(){})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant