1

I'm struggling to figure out how to pass a generic type which requires a parameter to a function only to have the function provide the param. Below is a contrived example to illustrate what I'm trying to do.

interface SingleItem<T> {
  body: T
}

interface MultiItem<T> {
  body: T[]
}

function requestImages<BodyType> () {
  ...
  return resp as BodyType<Image>;
}

requestImage<SingleItem>(); //<-- will throw because SingleItem requires a parameter
requestImage<MultiItem>();

I realize I could modify SingleItem and MutliItem to have a default for the type but I'm trying to learn the details of typescript, not necessarily circumvent this problem. Is there a way to pass around generics?

smallnoyd
  • 93
  • 7
  • Not directly supported in TypeScript; see [microsoft/TypeScript#1213](https://github.com/microsoft/TypeScript/issues/1213) for more info – jcalz Jul 02 '21 at 00:38
  • Currently the best workaround involves maintaining a registry of higher kinded types, like [this](https://tsplay.dev/m028Rw). If that works for you I could maybe write up an answer with it. – jcalz Jul 02 '21 at 00:49

0 Answers0