TypeScript 改进抽象类示例化错误消息的反馈

busg9geu  于 4个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(147)

建议

最近,我尝试在一个Ionic 2项目中使用一个用ES2015编写的库,并一直收到一个Cannot instantiate abstract class 'Foo'Foo是一个具体类,它继承了抽象类Bar,而Bar又继承了抽象类Baz。问题出在Bar.d.ts中的一个拼写错误(见下文),这使得Typescript认为Foo没有实现Bar和/或Baz的一些抽象方法(我仍然不确定是哪些),但我花了一天半的时间才弄清楚发生了什么。这个错误信息应该包含更多关于为什么Foo是抽象的信息:Foo is declared as abstractFoo 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.
OR
Error: Cannot instantiate abstract class 'Foo'. It does not implement abstract method(s): foo, bar.
OR
Error: 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'.

vcirk6k6

vcirk6k61#

本地化使得您提供的某些表单变得困难,但我认为这是可行的。我们还没有遇到不明显地抽象环境类问题。

相关问题