随笔分类 -  ts

摘要:有很多库它本身是js写的,但我们要在ts中使用它就会报错,因此我们需要声明一份文件来帮忙,在里面进行类型的添加,让其不报错 自定义声明文件 新建XXX.d.ts文件,这种文件就是一个声明文件 该声明文件里面没有任何的实际实现代码,只有类型声明,如 interface, function 或者 cla 阅读全文
posted @ 2022-05-19 18:37 见信 阅读(50) 评论(0) 推荐(0) 编辑
摘要:类型别名 // 使用type定义一个类型 type PlusType = (x: number, y: number) => number // let sum let sum: PlusType sum = (x, y) => { return x + y } 字面量 type Direction 阅读全文
posted @ 2022-05-19 18:00 见信 阅读(17) 评论(0) 推荐(0) 编辑
摘要:泛型 function echo<T>(a: T): T | undefined { return a } const result = echo<number>(123) function swap<T, U>(arr: [T, U]): [U, T] { return [arr[1], arr[ 阅读全文
posted @ 2022-05-19 17:44 见信 阅读(22) 评论(0) 推荐(0) 编辑
摘要:数字类型枚举 /** * 枚举 */ // 数字(number类型)枚举 enum Direction { Up, Down, Left, Right } // 每一个枚举的内部变量都会进行递增,且是一个双向赋值的过程! console.log(Direction.Up) // 0 console. 阅读全文
posted @ 2022-05-19 16:49 见信 阅读(30) 评论(0) 推荐(0) 编辑
摘要:联合类型 // 自动进行了类型推论,推论为string,因为我们的'str'是一个字符串 let str = 'str' // 此时再将它赋值为数字,就会报错 // str = 123 let numberOrString: number | string 类型断言 先强行将未知参数看做某个类型进行 阅读全文
posted @ 2022-05-19 16:22 见信 阅读(38) 评论(0) 推荐(0) 编辑
摘要:数组与元组 /** * 数组 */ let arrOfNumbers: number[] = [1, 2, 3] /** * 元组 */ let arr: [number, string] = [1, 'ljx'] arr.push(456) // arr.push(true) console.lo 阅读全文
posted @ 2022-05-19 16:00 见信 阅读(22) 评论(0) 推荐(0) 编辑
摘要:tsconfig.json是ts编译器默认使用的配置文件 tsconfig.json { "compilerOptions": { "strict": false, "target": "ES5" } } 阅读全文
posted @ 2022-05-18 21:47 见信 阅读(37) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示