1

I got return type from method but need the type of the generic of that type. I use method getDocument which return interface PDFPromise<T> and I need type of that T which is PDFDocumentProxy. I try:

type MyPDFPromise = ReturnType<typeof getDocument>; //get the PDFPromise<PDFDocumentProxy>
type MyPDFDocumentProxy = MyPDFPromise extends Promise<infer U> ? U : undefined;

But of course the PDFPromise don’t extend the Promise, I try with Object but with no luck. How could I get the type PDFDocumentProxy from that generic?

Thanks

kubo
  • 492
  • 7
  • 19
  • It should be extending `PDFPromise<>`, not `Promise<>`. – Jeff Mercado Oct 03 '19 at 06:17
  • Probably I don't understand something but I have the `MyPDFPromise` but not `MyPDFPromise`. When I change the type to that with generic it still not working. I checked that suggested duplicate page but they creating the type not getting from function. – kubo Oct 03 '19 at 06:42
  • `MyPDFPromise` is an alias to the type `PDFPromise`. The second line is using pattern matching to determine the type of `U`. `PDFPromise` does not extend `Promise`, but it does `PDFPromise`. To which the compiler could infer the type `U = PDFDocumentProxy`. – Jeff Mercado Oct 03 '19 at 06:50
  • Yes but I dont have the type `PDFPromise`. I don't know how to get it. – kubo Oct 03 '19 at 06:56
  • 1
    It has to be a type known to the compiler. If you cannot get that type, you won't be able to extract from it. – Jeff Mercado Oct 03 '19 at 06:58
  • Ok thanks, I thought I could get it from the type. – kubo Oct 03 '19 at 07:05

0 Answers0