二. OmitThisParameter<Type> 有了ThisParameterType获取 this 的类型,那么如何将一个定义了 this 参数类型的函数类型中的this 参数类型去掉呢? 这就是OmitThisParameter做的事情。 一句话概括,就是对于没有定义 this 参数类型的函数类型,直接返回这个函数类型,如果定义了 this 参数类型,就
classRectangle{privatew:number;privateh:number;constructor(w:number,h:number){this.w=w;this.h=h;}getArea(this:Rectangle){return()=>{returnthis.w*this.h;};}} 与前面定义的Rectangle长方形类不同,在getArea方法中,我们使用了this参数,之后this的类型是Rectangle类型,如下图所示: 在Rectangle长方形类get...
function foo(this: { name: string }, info: {name: string}) { console.log(this, info) } //2.OmitOmitThisParameter: 删除this参数类型, 剩余的函数类型 type PureFooType=OmiThisParameter<typeOf foo> 1. 2. 3. 4. 5. 3、ThisType 这个类型不返回一个转换过的类型,它被用作标记一个上下文的t...
用来去除函数类型中的 this 类型。如果传入的函数类型没有显式声明 this 类型,那么返回的仍是原来的函数类型。 // 第一个条件语句如果传入的函数参数没有 this 类型,则返回原类型; // 否则通过 infer 分别获取函数参数和返回值的类型构造一个新的没有 this 的函数类型,并返回这个函数类型。 type OmitThisParame...
*/typeOmitThisParameter<T>=unknown extends ThisParameterType<T>? T : T extends(...args: infer A)=>infer R ?(...args: A)=>R : T;/** * Marker for contextual 'this' type */interface ThisType<T>{ } 1. 2. 3. 4. 5.
ThisParameterType OmitThisParameter ThisType 快捷跳转 [玩转TypeScript工具类型(上)] [玩转TypeScript工具类型(中)] 一. ThisParameterType 提取一个函数类型显式定义的 this 参数,如果没有显式定义的 this 参数,则返回 unknown 。 这里有如下几个需要注意的点: ...
OmitThisParameter<Type> ThisType<Type> 社区工具类型 类型转换(Utility Types) Awaited<Type> // 有一个 Promise 对象,这个 Promise 对象会返回一个类型。 // 在 TS 中,我们用 Promise<T> 中的 T 来描述这个 Promise 返回的类型。 type APromiseType = Promise<string> type A = Awaited<ExampleType> /...
type OmitThisParameter2 = OmitThisParameter<string> // string 1. 2. 3. 4. 5. 16.ThisType 该工具类型比较特殊,它不是用来构造新类型的,而是用于定义对象字面量的方法中的 this 的类型。如果对象字面量的类型是 ThisType 或者是包含 ThisType 的交叉类型,那么在对象字面量的方法中的 this 类型为 T。
1 new (ParameterList) => Type new 是关键字,ParameterList 表示可选的构造函数形式参数列表类型,Type 表示构造函数返回值类型。 十、构造签名 构造签名的用法与调用签名类似。若在对象类型中定义了构造签名类型成员,那么我们称该对象类型为构造函数类型。构造签名的语法如下所示: 1 2 3 { new (ParameterList)...
hello.ts(15,20):error TS2345:Argumentof type'{ width: number; height: number; }'isnotassignable to parameter of type'Shape'.Property'name'ismissingintype'{ width: number; height: number; }'. 浏览器访问,输出结果如下: 箭头函数表达式(lambda表达式) ...