class Class {
}
const f0 = <T extends typeof Class> (c:T): T => {
return c
}
const call0 = f0 (Class) //ok
const f1 = <T extends typeof Class> (c:T): T => {
const a = new c()
return a //TS2322: Type 'Class' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'Class'.
}
const call1 = f1 (Class)
const f2 = <T extends typeof Class> (c:T):InstanceType<T> => {
const a = new c()
return a //TS2322: Type 'Class' is not assignable to type 'InstanceType '.
}
const call2 = f1 (Class)
The argument is typed as T, so why isn't that acceptable for the return type?
typeof Classto get a constructor which the question you mentioned is doesn't mention. Usingtypeof Classinstead of a generic constructor type is what caused the problemInstanceType<T>which the OP was already using. The linked answer does not answer the problem posted here which was how to create a generic constructor type. The solution to the OP's problem does not involve usingInstanceType. Please don't close a question linking to another question whose accepted answer does not address this post's problem.