Apologies for the less-than-descriptive title of this question, but sometimes it's easier to demonstrate with code rather than words. Here's a snippet that's causing issues, and I'm wondering why it's not behaving as expected:
interface Doer<U> {
doStuff: () => U;
}
class Promiser implements Doer<Promise<string>> {
doStuff() {
return Promise.resolve('foo');
}
}
export interface Type<T> extends Function {
new (...args: any[]): T;
}
function doYourThing<T extends Doer<U>, U>(doer: Type<T>): U {
return new doer().doStuff();
}
const result = doYourThing(Promiser);
// the type of "result" is `{}`
// but I'm aiming for it to be `Promise<string>`
You can tinker with the code above here.
I'm not entirely certain if this can be done, but I'm hoping that TypeScript can deduce the return type of doYourThing()
based on the argument being of type Doer<U>
and said argument actually being of type
Doer<Promise<string>>
.
Is my goal achievable (currently using TypeScript 2.4.2)?
If so, where am I faltering in my approach?