Skip to content

Forced to call super leading to undefined is not a constructor #21761

@jeffrose

Description

@jeffrose

TypeScript Version: 2.7.1

Search Terms:
super constructor undefined

Code

tsconfig.json

{
  "compilerOptions": {
    "declaration": true,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "lib": [ "esnext", "dom" ],
    "module": "es2015",
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "removeComments": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "target": "es5"
  }
}
type Constructor<T = {}> = new( ...args: any[] ) => T;
class Null extends null { }

interface MyInterface {
  myMixinMethod(): void;
  myClassMethod(): void;
}

const MyMixin = <T extends Constructor>( superclass: T ) => class extends superclass {
 myMixinMethod(): void {
    console.log( 'Called my mixin method' );
  }
};

abstract class AbstractMyClass extends MyMixin( Null ) implements MyInterface {
 abstract myClassMethod(): void;
}

class MyClass extends AbstractMyClass {
  constructor( public myProperty: string ){
    // If super() is not called, it throws the error below
    // [ts] Constructors for derived classes must contain a 'super' call.
    super();
  }
  myClassMethod(): void {
    console.log( 'Called my class method' );
  }
}

// If super() is called in MyClass, it throws the error below
// TypeError: undefined is not a constructor (evaluating '_super.call(this)')
const myClass = new MyClass( 'myValue' );

Expected behavior:
It should not require the super() call since parent class lacks a constructor.

Actual behavior:
It requires a call to super() which results in the error undefined is not a constructor.

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions