Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with Inheritance and TypeScript: X is not a constructor function type

I have this super class that I want two other classes to inherit from. The classes are listed below. When I compile, the two classes trying to inherit complain about the superclass (the give the same error): "[class file path (in this case A)] is not a constructor function type"

A.ts

export class A
{
    //private fields...

    constructor(username: string, password: string, firstName: string,
        lastName: string, accountType: string) 
    {
        // initialisation
    }
}

B.ts

import A = require('./A);
export class B extends A
{
    constructor(username: string, password: string, firstName: string,
        lastName: string, accountType: string) 
    {
        super(username, password, firstName, lastName, accountType);
    }
}

C.ts

import A = require('./A );
export class C extends A
{
    constructor(username: string, password: string, firstName: string,
        lastName: string, accountType: string) 
    {
        super(username, password, firstName, lastName, accountType);
    }
}

This is pretty simple, and yet Class C and B cannot compile. All the examples I have seen online do not have any other syntax for writing these classes/ constructor. I am trying to follow convention, but can't seem to get it to work.

like image 780
maria Avatar asked Feb 06 '16 17:02

maria


1 Answers

Replace

import A = require('./A');

with

import { A } from './A';

or

import moduleA = require('./A');

export class B extends moduleA.A {
  // ...
}
like image 181
Vadim Macagon Avatar answered Nov 17 '22 12:11

Vadim Macagon