随笔分类 - ts
摘要:###接口标识符 interface Swim { swimming: () => void } //作为标识符 const a: Swim = { swimming() { return 'sss' } } console.log(a); ###类实现接口 interface Swim { swi
阅读全文
摘要:###继承 class Person { name: string; constructor(name: string) { this.name = name; } } class Student extends Person { constructor(name: string) { super(
阅读全文
摘要:###函数作为参数时如何编写类型 function foo() { } function bar(fn: () => void) { fn() } bar(foo); ###定义常量时,编写函数的类型 函数类型固定的 () ⇒ void 括号里面写参数 void返回值可以是任何类型的,如果确定类型也
阅读全文
摘要:###1、typeof的类型缩小 type idType = number | string function printId(id: idType) { if (typeof id 'string') { console.log('字符串类型'); } else { console.log('数字
阅读全文
摘要:不用interface接口给对象的属性加限制 //obj的name必须是string类型 let obj = { name: <string>'张三' }
阅读全文
摘要:有的时候参数的类型可能会有很多,如果都列举出来的话代码看着会很臃肿。可以把它拆分出来,用type进行类型别名的设置。 function ss(id: string | number) { console.log(id); } ss(123); type idType = string | numbe
阅读全文
摘要:如果变量是 number、string、boolean类型。可以把null和undefined的类型值赋给变量 let a:boolean; a=null
阅读全文
摘要:https://www.jianshu.com/p/dd304d5cb3dc 类型断言 as 把一个大的范围断言成小的、精确的范围 type Method = 'GET' | 'POST' function dd(url: string, method: Method) { console.log(
阅读全文
摘要:###1、ts的安装 安装 node.js > 安装npm > 安装ts //安装ts npm install -g typescript 安装完成之后,在vscode里面建立一个.ts为后缀的文件,编写代码之后,使用命令将ts转化为js,这时候在当前目录下(与 .ts 同一目录)就会生成一个 .j
阅读全文