1

In TypeScript, there's the Parameters<T> helper type, which allows the extraction of a labelled tuple type representing the parameters that a function expects. For instance:

function func(a: number, b: string) {}

type Args = Parameters<typeof func> // [a: number, b: string]

Is there a way to convert from this type to an object type? I.e., obtain this:

type ArgsAsObject = ??? // should be { a: number, b: string }
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
  • 1
    This is not doable per se, see https://stackoverflow.com/questions/69687087/transform-named-tuple-to-object – Matthieu Riegler Mar 16 '23 at 13:19
  • That display is just a convenience, AFAIK it doesn't actually hold the keys a and b, at least not in any way available to you to create a mapped type. – Jared Smith Mar 16 '23 at 13:25
  • @JaredSmith For sure, it doesn't hold the keys at runtime, but the tuple type at compile time sure carries the name of the tuple elements with it. Too bad we don't seem to be able to extract them. – Jean-Philippe Pellet Mar 16 '23 at 13:34
  • @Jean-PhilippePellet I think the problem is the can of worms opened by equivalence: is tuple `[a: string]` assignable to `[b: string]`? I can totally understand why they just went with making the name in named tuples inaccessible, although it is unfortunate for this use case. – Jared Smith Mar 16 '23 at 13:38

0 Answers0