简体   繁体   English

Typescript 这是一个 function 参数/返回类型

[英]Typescript this as a function parameter/return type

I would like to be able to reference the type of the current object in a function's parameter/return type, such as the following:我希望能够在函数的参数/返回类型中引用当前 object 的类型,例如以下:

in .d.ts :.d.ts中:

interface Object {
    add(object: Partial<this>): this;
}

in .js :.js中:

Object.prototype.add = function(object) {
    Object.assign(this, object);
}

The intended behavior of this function is to only allow the parameter to contain properties that are already defined on the object via Partial<this> .此 function 的预期行为是仅允许参数包含已通过Partial<this>在 object 上定义的属性。 However, in attempted use such as但是,在尝试使用时,例如

document.body.style.add({
    fontSize: "12px"
});

TypeScript gives me the following error: TypeScript 给我以下错误:

Argument of type '{ fontSize: string; }' is not assignable to parameter of type `Partial<Object>`.
Object literal may only specify known properties, and 'fontSize' does not exist in type 'Partial<Object>'.

I assume that this is because the this types in my .d.ts actually just refers to Object because that's the containing interface for the function instead of recognizing the CSSStyleDeclaration type that document.body.style is.我认为这是因为我的.d.ts中的this类型实际上只是指Object因为这是 function 的包含接口,而不是识别document.body.style是的CSSStyleDeclaration类型。 How can I achieve a function like this, that uses the type of the object it's being applied to as parameters/return type?我怎样才能实现这样的 function ,它使用 object 的类型作为参数/返回类型? Is this possible in TypeScript?这在 TypeScript 中是否可行?

Disclaimer: it is considered bad practice to modify native prototypes this way.免责声明:以这种方式修改原生原型被认为是不好的做法 You may have some reason why it's okay to do this for your use cases, but you should probably avoid it.您可能有一些理由可以为您的用例执行此操作,但您可能应该避免它。 Anyway, let's move on.无论如何,让我们继续前进。


As you noticed, the polymorphic this type doesn't really work the way you want for the native interface types like Object , String , etc.. Even known subtypes of Object will see this rendered as Object instead of the known subtype. As you noticed, the polymorphic this type doesn't really work the way you want for the native interface types like Object , String , etc.. Even known subtypes of Object will see this rendered as Object instead of the known subtype.

As a workaround you can emulate the desired behavior by making the method generic and give it a generic this parameter :作为一种解决方法,您可以通过使方法通用并为其提供通用的this参数来模拟所需的行为:

interface Object {
  add<T>(this: T, object: Partial<T>): T;
}

So now if you call add() on some object of type CSSStyleDeclaration , the type checker will infer T to be CSSStyleDeclaration , and thus expect a Partial<CSSStyleDeclaration> as its parameter.所以现在如果你在一些CSSStyleDeclaration类型的 object 上调用add() ,类型检查器将推断TCSSStyleDeclaration ,因此期望Partial<CSSStyleDeclaration>作为它的参数。 So the following works as expected:因此,以下工作按预期工作:

document.body.style.add({
  fontSize: "12px"
});
// (method) Object.add<CSSStyleDeclaration>(
//   this: CSSStyleDeclaration, object: Partial<CSSStyleDeclaration>
// ): CSSStyleDeclaration

Playground link to code Playground 代码链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM