microsoft / microsoft/TypeScript

Improve feedback for abstract class instantiation error messages

Ouverte
#14,802 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Effort: Moderate Experience Enhancement Help Wanted Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire le diagnostic avec Baz.d.ts, Bar.d.ts, Foo.d.ts et otherProject.ts, en utilisant le comportement de TypeScript 2.0.9 décrit ici. Suivez l’erreur du compilateur lors de l’instanciation d’une classe abstraite et faites en sorte que le message identifie les méthodes abstraites ou la déclaration pertinente qui manque. C’est terminé lorsque l’exemple indique pourquoi Foo ne peut pas être instancié et correspond à l’un des messages attendus proposés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.