[英]Annotate a class using declaration file? — TypeScript
我有一個裝飾器,它將一個屬性添加到 class 中,如下所示:
function Service(constructor: any): any {
constructor.Instance = {};
}
@Service
class MyService {
}
The property will be static
as public static Instance: MyService
, but i have a bunch of classes, i just can't add public static Instance: ServiceType
, in every class. 我所做的是:
class BaseService<T> {
public static Instance: T; // Static members cannot reference class type parameters.ts(2302)
}
@Service
class MyService extends BaseService<MyService> {
}
哪個應該可以,但我得到了Static members cannot reference class type parameters.ts(2302)
,所以我試了這個:
function BaseService<T>() {
abstract class BaseService {
public static Instance: T;
}
return BaseService;
}
@Service
class MyService extends BaseService<MyService> { // Type '<T>() => typeof BaseService' is not a constructor function type.ts(2507)
}
這也不起作用,所以我想我們可以制作一個聲明文件,在其中我們可以將BaseService
class 聲明為一種類型,但我很困惑如何使用該類型? implements? extends?
,但 Type 無法擴展或實現。 由於Instance
已經存在於 class 中,我需要的是用該屬性注釋 class ,以便我可以像MyService.Instance
一樣訪問它,我不想在所有類中顯式添加。 有什么建議么?
我選擇了另一種方法,這是鏈接。 簡而言之,我正在做的裝飾器功能有點困難,但現在我有了另一種方法,所以它解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.