Error with Inheritance and TypeScript: X is not a constructor function type
16,790
Replace
import A = require('./A');
with
import { A } from './A';
or
import moduleA = require('./A');
export class B extends moduleA.A {
// ...
}
Author by
maria
Updated on July 03, 2022Comments
-
maria almost 2 years
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.
-
T.J. Crowder over 8 yearsWhere is
import...from
documented? I don't see it anywhere in typescriptlang.org/Handbook#modules. -
Vadim Macagon over 8 yearsThe handbook hasn't been updated in quite a while, I don't think it's wrong as such, but there are newer and better ways of doing some things that aren't mentioned in there. I suggest reading github.com/Microsoft/TypeScript/wiki/Breaking-Changes and github.com/Microsoft/TypeScript/wiki/… once you've familiarized yourself with the contents of the handbook.
-
maria over 8 years@VadimMacagon Will do. Thank you!
-
Vadim Macagon over 8 yearsI've updated my answer to show how the import style used in the handbook would be used.