在我在文档中找到的示例中,似乎 Typescript 中的某些继承可以为继承的父类提供额外的参数。我不确定这是否是 Typescript 中的新功能。
例如:
export class HttpStrategy extends PassportStrategy(Strategy, 'google') {
constructor(private readonly authService: AuthService) {
在这种情况下,父类 PassportStrategy 能够接受一个参数。但是,这似乎不是将传递给 PassportStrategy 的构造函数的参数,因为如果是这样的话,它会通过 super() 传递。
那么,这些参数是什么?在 Typescript 继承中父类的参数在哪里被使用?
PS:我尝试在线搜索它的文档,但我认为我没有使用此类参数的正确关键字。
最佳答案
您在 extends
子句中提供的内容可以是任何表达式,所以这里发生的事情是调用一个名为 PassportStrategy
的函数这两个参数,然后 HttpStrategy
扩展它返回的类,如下所示:
function Base(arg) {
return class {
doSomething() {
console.log("something: ", arg);
}
};
}
class Sub1 extends Base(1) {
}
const s1 = new Sub1();
s1.doSomething();
class Sub2 extends Base(2) {
}
const s2 = new Sub2();
s2.doSomething();
关于javascript - Typescript 继承的父类中的参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55156898/