建议
最近,我尝试在一个Ionic 2项目中使用一个用ES2015编写的库,并一直收到一个Cannot instantiate abstract class 'Foo'
。Foo
是一个具体类,它继承了抽象类Bar
,而Bar
又继承了抽象类Baz
。问题出在Bar.d.ts
中的一个拼写错误(见下文),这使得Typescript认为Foo
没有实现Bar
和/或Baz
的一些抽象方法(我仍然不确定是哪些),但我花了一天半的时间才弄清楚发生了什么。这个错误信息应该包含更多关于为什么Foo
是抽象的信息:Foo is declared as abstract
或Foo does not implement abstract methods ...
TypeScript版本: 2.0.9
代码Baz.d.ts
export as namespace Baz;
declare abstract class Baz {
abstract baz(): string;
}
Bar.d.ts
export as namespace BarTypo;
declare abstract class Bar extends Baz {
abstract foo(): void;
abstract bar(): string;
baz(): string;
}
export { Bar };
Foo.d.ts
import { Bar } from './bar.d';
export as namespace Foo;
declare class Foo extends Bar {
constructor(opts?);
foo(): void;
bar(a: string): string;
}
export { Foo };
otherProject.ts
import { Foo } from 'library';
export class Service {
foo: Foo;
constructor() {
this.foo = new Foo({ opt: true });
}
}
预期行为:
Error: Cannot instantiate abstract class 'Foo'. It does not implement abstract method(s): baz.
ORError: Cannot instantiate abstract class 'Foo'. It does not implement abstract method(s): foo, bar.
ORError: Cannot instantiate abstract class 'Foo'. It does not implement abstract method(s): baz (from Baz); foo, bar (from Bar).
实际行为:
Error: Cannot instantiate abstract class 'Foo'.
1条答案
按热度按时间vcirk6k61#
本地化使得您提供的某些表单变得困难,但我认为这是可行的。我们还没有遇到不明显地抽象环境类问题。