I am currently working on defining a 'Definition' type in Typescript. In this case, a Definition could be either a class constructor or an object. Here is the code snippet that illustrates my approach:
if (this._isConstructor(definition)) {
return new definition(...args); // if it is a class, instantiate it
}
return definition; // if it is an object, just return it
The initial way I defined my type was as follows:
type Definition = {
new (arg?: object): object | object
}
This method works but appears somewhat messy. As a result, I decided to split it into separate types:
type Definition = {
Cstruct | object
}
type Cstruct = new (arg?: object): object
However, this modification resulted in an error message stating:
Cannot use 'new' with an expression whose type lacks a call or construct signature
whenever attempting to utilize the 'new' keyword.