Check out this example from my sandbox:
class A {
doSomething() {}
}
class B {}
const collection = {
a: new A(),
b: new B()
}
const findInstance = <T>(list: any, nonInstance: T): T => {
for (const item in list) {
if (list[item] instanceof (nonInstance as any)) {
return list[item] as any
}
}
throw new Error('Unable to locate instance')
}
const a: A = findInstance(collection, A)
a.doSomething()
The type error I am encountering is:
Property 'doSomething' is missing in type 'typeof A' but required in type 'A'.
Instead of receiving typeof A
, what I need is A
.
Click here to view the TypeScript playground.