Microsoft/TypeScript

Improve feedback for abstract class instantiation error messages

Open

#14,802 opened on 2017年3月22日

GitHub で見る
 (1 comment) (0 reactions) (0 assignees)TypeScript (48,455 stars) (6,726 forks)batch import
Effort: ModerateExperience EnhancementHelp WantedSuggestion

説明

SUGGESTION

Recently, I was trying to use a library written in ES2015 in an Ionic 2 project, and kept getting a Cannot instantiate abstract class 'Foo'. Foo is a concrete class that extends abstract class Bar, which extends abstract class Baz. The problem turned out to be a typo in Bar.d.ts (see below), which (I believe) made Typescript think that Foo did not implement some abstract methods of Bar and/or Baz (I'm still not sure which), but it took me a day and half to figure out what was going on. This error message should have more information about why Foo is abstract: Foo is declared as abstract or Foo does not implement abstract methods ...

TypeScript Version: 2.0.9

Code

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 });
  }
}

Expected behavior: 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).

Actual behavior: Error: Cannot instantiate abstract class 'Foo'.

コントリビューターガイド