Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type X is not a 'constructor function type' (TS2507)

While designing a definition file, I came across this error (TS2507).

How can I specify a type to be a 'constructor function type' ?

like image 558
Bruno Grieder Avatar asked Nov 06 '15 17:11

Bruno Grieder


People also ask

How do you fix not a constructor?

We tried to instantiate a value that is not a constructor as a constructor, which caused the error. To solve the "TypeError: 'X' is not a constructor" in JavaScript, make sure to only use the new operator on valid constructors, e.g. classes or constructor functions.

Is not a constructor in JavaScript?

The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor, but that object or variable is not a constructor.

Which of the following is not a built in constructor in JavaScript?

Javascript has provided some built-in constructors. Those built-in functions include object(), string(), number, etc. We cannot include Math object in those built-in constructors because Math is a global object. The 'new' keyword cannot be used with Math.

What is the constructor in JavaScript?

A constructor is a special function that creates and initializes an object instance of a class. In JavaScript, a constructor gets called when an object is created using the new keyword. The purpose of a constructor is to create a new object and set values for any existing object properties.


1 Answers

If you are defining an interface, you can declare that it is a constructor like such:

interface SomeInterface {
  new(someParam: any): SomeInterface
}

This is useful when you are defining typings for already existing JS libraries. See this SO answer for more details.

like image 154
JKillian Avatar answered Nov 05 '22 13:11

JKillian