typescript - 仅在函数或构造函数实现中允许参数初始化

标签 typescript visual-studio-2015

在下面它提示我由于可选参数(“=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/

相关文章:

c++ - 尽管已安装,Visual Studio 仍无法打开源文件

c# - 使用反射打印自己的方法名、类库(可移植)

c# - Visual Studio 2015 中的 MonoGame 模板

c# - 未找到与约束契约(Contract)名称 microsoft.visualstudio.portable library Android Portable Project 匹配的导出

typescript - 方括号是什么意思,字段应该在 typescript 中的位置?

mysql - 即时聊天应用检索聊天记录

javascript - 使用 TypeScript 和 Angular $http 服务进行跨域请求

c# - 我可以在没有安装 Visual Studio 的 Windows 服务器上构建 .NET Core 应用程序吗?

angular - typescript 减法未按预期工作

typescript - 如何用同一对象中的另一个属性约束一个属性的类型?