在下面它提示我由于可选参数(“=null”部分)而出错。 我希望我可以在抽象类中声明可选参数,但为什么 typescript 不允许我这样做?
abstract class Atom extends DatalogElement {
abstract toStringFormula(elem: DatalogElement.StringFormat, variableMap: Collections.Dictionary<string, Collections.Dictionary<number, VariableMap>>=null): string
}
最佳答案
因为 abstract
函数不能有函数 bodies
并且默认参数只有在你有函数体时才能实现。
允许:
abstract class Atom {
abstract foo(x:any);
}
错误:
abstract class Atom {
abstract foo(x:any=null); // Cannot have an implementation
}
错误:
abstract class Atom {
abstract foo(x:any){ // Error cannot have implementation
}
}
允许:
abstract class Atom {
abstract foo(x?:any);
}
关于typescript - 仅在函数或构造函数实现中允许参数初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34148254/