# TypeScript 是什么
- TypeScript 是由 Microsoft 开发的一种开源编程语言,它扩展了 JavaScript,增加了静态类型系统和类、接口等面向对象的特性。TypeScript 代码在编译时会被转换为纯 JavaScript 代码,可以在任何支持 JavaScript 的环境中运行。
# 环境安装
| |
| npm install typescript -g |
- 创建
ts 文件,比如 index.ts ,然后编写测试代码
| let str:string='Hello World'; |
| console.log(str); |
| |
| npm install ts-node -g |
| |
| npm init -y |
| |
| npm install @types/node -D |
| |
| ts-node index.ts |
# arguments 对象
- arguments 对是一个类数组对象,作用域在方法内部时,包含了当前函数被调用时传入的所有参数,也可以转换为数值,使用 arguments 调试的时候很方便
| function foo(...params: any[]) { |
| console.log(arguments); |
| const args = Array.from(arguments); |
| console.log(args); |
| } |
| foo(123, "hello", true); |
# Any 类型和 Unknown 类型
# 相同点
- any 和 unknown 都是顶级类型,是 Object 类型的父类
# 不同点
- unknown 只能赋值给自身或者 any
- unknown 表示不确定类型的参数,不允许调用对象的任何属性和方法,否则会出现编译错误,可以只用于方法中的输入或输出,是安全型的 any
# Never 类型
- 用于从来都不会返回值的函数 (比如死循环),或者无论如何都会抛出异常的函数
| const loop = (): never => { |
| while (true) { } |
| } |
| const error = (): never => { |
| throw new Error(); |
| } |
# Object 和 object 类型
- Object(大写)和 Java 和 C# 一样都包含了所有类型,可以存放任意值
- object(小写)只能存放引用类型或者函数
| let a1:Object = 123 |
| let a2:Object = true |
| let a3:Object = '123' |
| let a4:Object = {} |
| let a5:Object = [] |
| let a6:Object = ()=>123 |
| |
| let b1:object = 123 |
| let b2:object = true |
| let b3:object = '123' |
| let b4:object = {} |
| let b5:object = [] |
| let b6:object = ()=>123 |
# Interface
# 多个同名的 Interface 会进行合并
| interface Person { |
| name: string; |
| } |
| interface Person { |
| age: number; |
| } |
| let p1: Person = { |
| name: "111", |
| age: 18 |
| } |
# 可空类型?和只读类型 readonly
| interface Person { |
| name: string; |
| age?: number; |
| readonly id:number |
| readonly print:()=>{} |
| } |
| |
| let p1: Person = { |
| name: "111", |
| id:1, |
| print:()=>{return true} |
| } |
| |
| p1.id=2; |
| p1.print=()=>{return false}; |
# 继承 extends
| interface ClassA { |
| name: string; |
| } |
| interface ClassB { |
| age: number; |
| sex: boolean |
| } |
| |
| interface Person extends ClassA, ClassB { |
| print: () => {}; |
| } |
# 接口定义函数
| interface Show{ |
| (name:string,age:number):string |
| } |
| |
| const fn:Show=(name:string,age:number)=>`${name}:${age}` |
| |
| console.log(fn('张三',18)); |
# Interface 索引签名
- 实现 Interface 的对象的属性数量必须与接口的属数量保持一致,如果只是用到 Interface 中的一小部分属性可以使用属性签名
| interface Person{ |
| name:string, |
| age:number, |
| |
| [propName:string]:any |
| } |
| let p1:Person={ |
| name:'111', |
| age:18, |
| sex:0 |
| } |
| p1.address='2222'; |
| interface Product { |
| name: string; |
| price: number; |
| stock: { |
| [size: string]: number; |
| }; |
| colors: { |
| [color: string]: boolean; |
| }; |
| } |
| |
| const hoodie: Product = { |
| name: 'Hoodie', |
| price: 50, |
| stock: { |
| 'S': 5, |
| 'M': 8, |
| 'L': 3, |
| }, |
| colors: { |
| 'Red': true, |
| 'Blue': false, |
| }, |
| }; |
| |
| console.log(hoodie.stock['S']); |
| console.log(hoodie.colors['Red']); |
# 数组定义
| |
| let arr1: number[] = [1, 2, 3, 4]; |
| |
| let arr2: Array<number> = [1, 2, 3, 4]; |
| |
| interface Person { |
| name: string |
| } |
| let arr3: Person[] = [{ name: '111' }]; |
| let arr4: Array<Person> = [{ name: '222' }]; |
| |
| let arr5:number[][]=[[1],[2],[3]]; |
| let arr6:Array<Array<number>>=[[1],[2],[3]]; |
| |
| let arr7:any[]=[123,'abc',true]; |
| let arr8:[number,string,boolean,{}]=[123,'abc',true,{name:'333'}] |
| |
| function fn(...args:number[]){ |
| console.log(args); |
| console.log(arguments); |
| } |
| fn(1,2,3,4,5); |
# 函数定义
| |
| function fn1(a: number, b: number): number { |
| return a + b; |
| } |
| |
| const fn2 = (a: number, b: number): number => { |
| return a + b; |
| } |
| |
| const fn3 = (a: number = 1, b: number = 2): number => { |
| return a + b; |
| } |
| |
| const fn4 = (a: number, b?: number): number => { |
| return a + (b ?? 0); |
| } |
| console.log(fn4(1)); |
# 函数重载
- 函数重载有点复杂,不像 Java 或者 C# 等可以直接写同名的实现方法,还是少用为好
| |
| function message(options: object): void; |
| function message(text: string, onClose?: Function): void; |
| function message(text: string, mode?: string, duration?: number): void; |
| function message(text: string, duration?: number, onClose?: Function): void; |
| |
| |
| function message( |
| param1: string | object, |
| param2?: number | Function | string, |
| param3?: Function | number |
| ): string { |
| if(typeof param1=='string') return param1; |
| if(typeof param1=='object') return param1.toString(); |
| |
| return ''; |
| } |
| |
| |
| message({ |
| mode: "mode", |
| text: "msg", |
| onClose: () => {}, |
| duration: 3000, |
| }); |
| |
| |
| message("msg"); |
| message("msg", () => {}); |
| message("msg", "mode"); |
| message("msg", "mode"); |
| message("msg", "mode", 3000); |
| message("msg", 3000, () => { }); |
# 联合类型 | 和交叉类型 &
| |
| const fn1 = (type: number | string): string => { |
| return type.toString(); |
| } |
| console.log(fn1('666')); |
| |
| interface Person { |
| name: string, |
| age: number |
| } |
| interface Man { |
| hobby: string |
| } |
| const fn2 = (man: Person & Man):void => { |
| console.log(man); |
| } |
| fn2({ |
| name:'张三', |
| age:18, |
| hobby:'唱跳rap' |
| }) |
# 断言
| const fn = (num: number | string): void => { |
| |
| console.log((num as string).length); |
| console.log((<string>num).length); |
| } |
| fn('123456') |
| fn(123456) |
| |
# Class 类
| interface Person { |
| name: string; |
| |
| readonly id: number; |
| show(): void; |
| } |
| |
| class Man implements Person { |
| name: string; |
| id: number; |
| |
| constructor(name: string, id: number) { |
| this.name = name; |
| this.id = id; |
| } |
| |
| public sing(): void { |
| } |
| private dance(): void { |
| } |
| protected rap(): void { |
| } |
| |
| |
| show(): void { |
| console.log(`练习时间长达${this.time}的个人练习生${this.name}`) |
| } |
| |
| |
| static basketball() { |
| this.music(); |
| |
| } |
| static music() { |
| console.log("登登登登~~~"); |
| } |
| |
| |
| private _time: number = 2.5; |
| get time() { |
| return this._time; |
| } |
| set time(time: number) { |
| this._time = 2.5; |
| } |
| } |
| |
| const m1: Man = new Man("蔡徐坤", 18); |
| m1.time = 2.5; |
| m1.show(); |
| Man.basketball(); |
# 元组
| |
| let arr1: [number, string] = [1, '2'] |
| arr1.push(3); |
| console.log(arr1); |
| |
| |
| const arr2: readonly [number, boolean] = [1, false]; |
| |
| |
| console.log(arr2); |
| |
| |
| type Person = [name: string, age: number, sex: boolean]; |
| const arr3: Person = ['张三', 18, true]; |
| console.log(arr3[0], arr3[1], arr3[2]); |
| const [names, age, sex] = arr3; |
| console.log(names, age, sex); |
# ts 编写防抖功能
| let timer: any; |
| let clickTest = () => { |
| clearTimeout(timer); |
| timer = setTimeout(() => { |
| console.log('clicked'); |
| timer = null; |
| }, 500) |
| } |