TypeScript: Class Inheritance is Not ES6 Spec Compliant

The following should log true but logs false instead:

class Foo {}
class Bar extends Foo {}

console.log(Object.getPrototypeOf(Bar) === Foo);

This is due to how the __extends helper is implemented. It generates this:

var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};

An example of a correctly functioning extends implementation can be found in the 6to5 compiler. Here is their implementation:

var _extends = function (child, parent) {
    child.prototype = Object.create(parent.prototype, {
      constructor: {
        value: child,
        enumerable: false,
        writable: true,
        configurable: true
      }
    });
    child.__proto__ = parent;
  };

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 25 (23 by maintainers)

Most upvoted comments

So, the reason I brought this up is because it relates to annotations. I’m writing a library for working with AtScript metadata annotations. Annotations are stored directly on the function that they annotate. On some occasions you want to search the class hierarchy for any annotations of a certain type. To do this, you need to use Object.getPrototypeOf(Derived) recursively until you find the annotation you are looking for or hit null. If/when TypeScript adds annotations, and developers want to start working with them, there will be a greater need to handle this.

For my own case, I am only targeting Evergreen browsers, so old IE and ES3 are not a concern of mine.