#How to get the ReturnType of a function with a generic parameter

6 messages · Page 1 of 1 (latest)

oak stream
#

I have a simple generic function which I'd like to get the return type of.

  if ('a' in a) {
    return 1;
  }

  return 2;
}```

I want to do something like this: 

```type Something = ReturnType<typeof testing<{ a: 1 }>> // Should be 1```

That doesn't work, because `ReturnType` can't take a generic. How can I make it work?
slow creek
#

@oak stream Your original code does work though?

topaz ravenBOT
#
declare const testing: <T extends { a: number } | { b: number }>(
    a: T,
) => T extends { a: number } ? 1 : 2

type Something = ReturnType<typeof testing<{ a: 1 }>>
//   ^? - type Something = 1
south crown
#

Might need to bump your TS version - that was a TS 4.7 feature: