I'm trying to write a function getDocument
which would do the following:
interface DocumentReference<T> {
get(): Promise<T>;
}
interface ParentA {
foo: DocumentReference<User>;
}
interface ParentB {
bar: DocumentReference<Company>;
}
// getDocument<ParentType>(parent: ParentType, fieldName: keyof ParentType): Promise<???>
getDocument(parentA, 'foo') // return a Promise<User>
getDocument(parentB, 'bar') // return a Promise<Company>
I've been trying many different combinations of generic signatures for getDocument
but can't figure out how to restrict fieldName
so that it corresponds to a field value of type DocumentReference
. I don't know how to extract the generic parameter of DocumentReference<T>
into the return type either.
Is this doable in typescript? If yes, any pointers?