我正在寻找一种方法将父构造函数参数的类型传递给子构造函数,例如
class B extends A {
constructor (input) {
super(input);
}
}
我试过了
class B extends A {
constructor (input : ConstructorParameters<typeof super>) {
super(input);
}
}
但我从 eslint 'super' is not defined.
最佳答案
我认为你必须传递为 ConstructorParameters<typeof A>[0]
作为构造函数参数类型。
class A {
title: string;
constructor(title: string) {
this.title = title;
}
}
class B extends A {
constructor(title: ConstructorParameters<typeof A>[0]) {
super(title);
}
}
您可以尝试在评论中建议的另一种解决方案:
constructor(title: ConstructorParameters<typeof A>) {
super(...title);
}
关于typescript - 如何将 super 的类型分配给子构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69220098/