microsoft / microsoft/TypeScript

Convert to class declaration quick fix misses superclass

Open
#31,514 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.4.3

Search Terms:

  • class declaration quick fix
  • 80002

Code

This code is a mix of ES2015- and function-style classes:

class A {
  constructor() {
    this.a = 'a';
  }
  doAThing() {
    console.log(this.a);
  }
}

/**
 * @constructor
 * @extends {A}
 */
function B() {
  this.b = 'b';
}
B.prototype = new A();

B.prototype.doBThing = function() {
  console.log('b: ', b);
};

The TS language services offer a quickfix to convert B into an ES2015 class. Great!

Expected behavior:

I'd hope that one of (or both of) the @extends and B.prototype = new A(); would result in B extends A:

class B extends A {
  constructor() {
    super();
    this.b = 'b';
  }
  doBThing() {
    console.log('b: ', b);
  }
}

Actual behavior:

The quick fix produces this code:

/**
 * @constructor
 * @extends {A}
 */
class B {
  constructor() {
    this.b = 'b';
  }
  doBThing() {
    console.log('b: ', b);
  }
}
B.prototype = new A();

TypeScript flags an error on the last line:

(property) B.prototype: B
Type 'A' is missing the following properties from type 'B': b, doBThingts(2739)

Playground Link: no quickfixes on the playground, alas!

Related Issues:

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 with the TypeScript language service quick fix that converts a function-style class into an ES2015 class, using the supplied example as the reproduction. Check how the @extends annotation and B.prototype = new A() are handled, then verify that the converted class preserves the superclass relationship and no longer produces the reported type error.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.