Skip to content

Instantly share code, notes, and snippets.

@imaximix
Last active September 4, 2019 08:17
Show Gist options
  • Save imaximix/b203c1ba78f7b130b54ff1a42a036b81 to your computer and use it in GitHub Desktop.
Save imaximix/b203c1ba78f7b130b54ff1a42a036b81 to your computer and use it in GitHub Desktop.
interface MyCustomType<T> {
prop: Promise<T>
}
interface MyBlaBlaType {
prop: string
}
interface MyObj {
[key: string]: (...args: any) => any
}
function fn<M extends MyObj>(bla: M): {
[N in keyof M]: ReturnType<M[N]> extends MyCustomType<infer T>
? (...args: Parameters<M[N]>) => Promise<T> // I would like to tell TS that this will return Promise<what ever MyCustomType has as a generic )
: M[N]
} {
// Implementation detail
return 123 as any;
}
// usage:
var y = fn({
x1:
(): MyCustomType<string> => ({ prop: Promise.resolve("asd") }),
x2:
(): MyCustomType<number> => ({ prop: Promise.resolve(123) }),
x3:
(): MyBlaBlaType => ({ prop: "blabla" })
}); // I would want to enforce y to be: { x1: Promise<string>, x2: Promise<number>, x3: MyBlaBlaType }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment