Microsoft/TypeScript

Improve feedback for abstract class instantiation error messages

Open

#14.802 geöffnet am 22. März 2017

Auf GitHub ansehen
 (1 Kommentar) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
Effort: ModerateExperience EnhancementHelp WantedSuggestion

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

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'.

Contributor Guide