microsoft / microsoft/TypeScript

Improve feedback for abstract class instantiation error messages

Open
#14,802 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Effort: Moderate Experience Enhancement Help Wanted Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
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'.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the diagnostic with Baz.d.ts, Bar.d.ts, Foo.d.ts, and otherProject.ts, using the TypeScript 2.0.9 behavior described here. Trace the compiler's abstract-class instantiation error and make the message identify the relevant missing abstract methods or declaration. Done means the example reports why Foo cannot be instantiated, matching one of the proposed expected messages.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.