3

Question

Is there a general way to implement a generic type that takes a type T and a generic type G<> and outputs the former passed into the latter G<T>?

Goal

/**
 * @template G, T
 * @typedef {G<T>} Transform<G,T>
 */

/** 
 * @typedef {Transform<Array, string>} StringArray
 * @typedef {Transform<Promise, string>} StringPromise
 */

This doesn't seem to work unfortunately.

Limited functionality

What does work is this, but it needs the possible generic types to be specified.

/**
 * @template G, T
 * @typedef {G extends Promise ? Promise<T> : G extends Array ? Array<T> : unkown } Transform<G,T>
 */

/** 
 * @typedef {Transform<Array, string>} StringArray 
 * @typedef {Transform<Promise, string>} StringPromise 
 */
Brother58697
  • 2,290
  • 2
  • 4
  • 12
  • Is this Typescript ir Javascript+JSDoc question? – Dimava May 05 '23 at 09:08
  • 1
    There is no direct support for *higher kinded types* in TypeScript; see the [linked question](https://stackoverflow.com/questions/60007436/higher-order-type-functions-in-typescript) for more information; some of the answers there mention the various workarounds/approaches to simulating them (although I still think they are too cumbersome to recommend in general) – jcalz May 05 '23 at 13:02
  • @jcalz Thank you. I hope this moves forward somehow given how old the issue is – Brother58697 May 05 '23 at 14:39

0 Answers0