Attach a generic <T> operator using FunctionConstructor

I have a TS function that deserializes JSON into some type / object. You give it a constructor for a type function and try to restore that type from JSON.

It looks like this.

export function deserializeJSON<T>(JSONString: string, type?: FunctionConstructor, ... 

I would like to express that the argument of type: FunctionConstructor is a constructor from type <T> that can be used with some expression?

UPDATE

deserializeJSON call deserializeJSON

 const serializedDate = JSON.stringify(new Date); const deserializedDate = deserializeJSON<Date>(serializedDate, Date); 

EDIT

I just found out that FunctionConstructor means a Function constructor, so it's better to mark the type argument as type: Function , since all the constructors are functions, but then I cannot create an instance of new type without the fill type before <any> . I would like to express something like <T>.constructor .

EDIT

I defined the interface:

 interface Constructor<T> extends Function{ new (): T; new (...args: Array<any>): T; (...args: Array<any>): T; prototype: T; } 

This is basically a universal newbie to Function , but this type of "... is not assigned to a parameter of type DateConstructor" or any other type constructors, so it's best to stay with type: Function because it accepts all the constructors.

+5
source share
1 answer

Answering my own question.

This interface requires more tests, but it seems to work:

 interface Constructor<T> extends Function { new (): T; new (...args: Array<any>): T; prototype: T; } 
+2
source

All Articles