let bool:boolean = true; let num:number = 10; let str:string = 'hello world';
let tuple:[string,number,boolean] = ['zf',10,true]; // 像元组中增加数据,只能增加元组中存放的类型,就是说你只能Push进去你定义过的类型,也不可以通过索引去赋值(不可以tuple[2]=false这样写) tuple.push('回龙观');
let arr1:number[] = [1,2,3]; let arr2:string[] = ['1','2','3']; let arr3:(number|string)[] = [1,'2',3]; // 这个叫做联合类型,要记得加括号哦~ let arr4:Array<number | string> = [1,'2',3]; // 泛型方式来声明 let arr5:any[] = ['', 1, {}] // 如果是无规律的,没得办法咯~
ts最终会被编译成JS,是没有类型的,枚举只是在开发时候使用
写好以后,经过编译,会变成一个对象
枚举可以支持反举,但是限于索引,会根据上一个的值进行自动推断
大写是规范
使用赋值声明(const)的话,不会生成一个对象更简洁
enum USER_ROLE { USER, // 默认从0开始 ADMIN, MANAGER } // {0: "USER", 1: "ADMIN", 2: "MANAGER", USER: 0, ADMIN: 1, MANAGER: 2}
// 编译后的结果 (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 USER_ROLE { USER, ADMIN, MANAGER, } console.log(USER_ROLE.USER)// console.log(0 /* USER */);
let arr:any = ['jiagou',true,{name:'zf'}]
let name:number | boolean; name = null;
let a:void; a = undefined;
function error(message: string): never { throw new Error("err"); } function loop(): never { while (true) { } } function fn(x:number | string){ if(typeof x == 'number'){ }else if(typeof x === 'string'){ }else{ console.log(x); // never } }
const s1 = Symbol('key'); const s2 = Symbol('key'); console.log(s1 == s2); // false
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))
let create = (obj:object):void=>{} create({}); create([]); create(function(){})
let name; // 类型为any name = 'zhufeng' name = 10;
let name = 'zhufeng'; // name被推导为字符串类型 name = 10;
let bool1:boolean = true; let bool2:boolean = Boolean(1); let bool3:Boolean = new Boolean(2);
let name:string | number // 联合类型 console.log(name!.toString()); // 公共方法 name = 10; console.log(name!.toFixed(2)); // number方法 name = 'aaaaa'; console.log(name!.toLowerCase()); // 字符串方法
let ele: HTMLElement | null = document.getElementById('#app'); ele!.style.color = 'red'; // 断定ele元素一定有值
let name: string | number; (name! as number).toFixed(2); // 强制 ((<number>name!).toFixed(2));
尽量使用第一种类型断言因为在react中第二种方式会被认为是jsx语法
双重断言
let name: string | boolean; ((name! as any) as string);
type Direction = 'Up' | 'Down' | 'Left' | 'Right'; let direction:Direction = 'Down';
function sum(a: string, b: string):string { return a+b; } sum('a','b')
可以用来限制函数的参数和返回值类型
通过表达式方式声明
type Sum = (a1: string, b1: string) => string; let sum: Sum = (a: string, b: string) => { return a + b; };
let sum = (a: string, b?: string):string => { return a + b; }; sum('a'); // 可选参数必须在其他参数的最后面
let sum = (a: string, b: string = 'b'): string => { return a + b; }; sum('a'); // 默认参数必须在其他参数的最后面
const sum = (...args: string[]): string => { return args.reduce((memo, current) => memo += current, '') } sum('a', 'b', 'c', 'd')
function toArray(value: number): number[] function toArray(value: string): string[] function toArray(value: number | string) { if (typeof value == 'string') { return value.split(''); } else { return value.toString().split('').map(item => Number(item)); } } toArray(123); // 根据传入不同类型的数据 返回不同的结果 toArray('123');