f.e.,我有
- declare class Foo extends Bar {
- foo: number
- }
如何声明foo具有默认值(或初始值),例如60.
我试过了
- declare class Foo extends Bar {
- foo: number = 60
- }
但我得到一个错误
- 4 foo: number = 60
- ~~
- path/to/something.js/Foo.d.ts/(4,28): error TS1039: Initializers are not allowed in ambient contexts.
解决方法
您的程序尝试执行两个相互矛盾的任务.
>它试图声明一个类存在但实际上是在别处/否则实现的.
>它试图定义该实现.
您需要确定要执行哪些任务,并通过删除初始化程序或声明修饰符来相应地调整程序.