1.首先安装typeScript
npm install typescript
2.方便学习安装一个@types/node方便编译ts
npm install @types/node
可以直接使用命令 ts-node index.ts
准备工作完成!!!
类型的等级
1:any unknown //顶级的类型
2: Object
3: Number String Boolean
4: number string boolean
5: 1 '12313' false
6: never
如何类型都能赋值给any
let names: any = [];names = 1;let a: number = 1;names = a;a = names;
注意:unknown 只能赋值给any类型和unknown类型
let age: unknown = [];age = names;age = a;// a = age;names = age;
注意:unknown不能读取属性
let datas: unknown = { name: '12313' };console.log(datas.name)//错误
只有[],{],()=>{}可以赋值给object
// let data1:object = 1231; //错误// let data2:Object = '13321';//错误// let data3:Object = false;//错误let data4: object = [];let data5: object = {};let data6: object = () => 123;console.log(data6)