Home:ALL Converter>Type of Class in typescript?

Type of Class in typescript?

Ask Time:2022-09-09T00:30:01         Author:andré michelle

Json Formatter

I am trying to type a method, where I pass a class and a factory as an argument. Both should strongly point to type T, but the compiler is okay with the following simplified test. Is there a stronger approach to type a class reference?

type Class<T> = new (...args: any[]) => T
class A { }
class B { }
const test = <T>(type: Class<T>, factory: () => T) => { }
test(A, () => new B()) // why is this accepted?

I tried all answers from Is there a type for "Class" in Typescript? And does "any" include it?

Author:andré michelle,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/73652379/type-of-class-in-typescript
yy