2

I need this type

type Sample = [a: string, b: number]

to be converted to

type Sample2 = { a:string, b:number}

I need this to pass arguments of a function in a paramenter, in form of an object

function example(otherFunctionParameters: Parameters<typeof otherFunction>){}

I don't want to pass an array in the parameter.

How can I achieve this ?

DmitriyC
  • 33
  • 4
  • `type Sample = [a: string, b: number]` this is not a valid javascript statement. Are you able to run this via js compiler? Or is this like `var Sample = "[a: string, b: number]"` – Dipak Feb 10 '22 at 03:33
  • AFAIK, labels cannot be manipulated in the type system (but maybe jcalz will teach us something new again today), so for your first type `type Sample = [a: string, b: number]`, you can't get the `a` and `b` labels from the tuple to use as keys of your object (you have to supply them yourself). – jsejcksn Feb 10 '22 at 03:34
  • If [this](https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&target=99&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true#code/GYVwdgxgLglg9mABHKALApgJwGLmvJACgEMAuRAZykxjAHMAaRAI3LBAFtmsBKRAbwC+AWABQYqAE8ADukQBRAB7EO0gDboA8swBW6aAAVimFYgC8AsYkRlERkx3RQsFADxTZcYMjRZckWAQAPgBtAAYAXQYrFnJ7FScXdxl0Lx8MHDxAsFCARiixQQBuMTFQAIJEdGVVDURCYBhMKniOciUVdS1dfShWvn4YiAQqAWImZkFzREbmvuMVEtFrYiLEAHp1ympaOhjmNc3Edi4sQqA) meets your needs, I can make it into an answer. – jsejcksn Feb 10 '22 at 03:42
  • Unfortunately, this is not what I was looking for.Even tho it works for that exact example, I'm trying to make it work for any function with any parameter names. Thanks a lot! – DmitriyC Feb 10 '22 at 03:48
  • 1
    Confirmed: [it's not possible](https://github.com/Microsoft/TypeScript/issues/26019#issuecomment-568236218) – jsejcksn Feb 10 '22 at 03:52

1 Answers1

1

Because you can't manipulate tuple labels in the type system, the best you can do is manually supply a tuple of property keys, one for every parameter in the original function.

By adapting some utilities from this answer by jcalz, here's a generic utility that will give you a mapped object for the parameters in a function: You supply the function type and a tuple of labels for each parameter:

TS Playground

type ZipTuples<Keys extends readonly any[], Values extends readonly any[]> = {
  [K in keyof Keys]: [Keys[K], K extends keyof Values ? Values[K] : never];
};

type ZipTuplesAsObject<
  Keys extends readonly PropertyKey[],
  Values extends readonly any[],
> = { [T in ZipTuples<Keys, Values>[number] as T[0]]: T[1] };

type ParamsAsObject<
  Fn extends (...params: readonly any[]) => any,
  Keys extends readonly PropertyKey[],
> = ZipTuplesAsObject<Keys, Parameters<Fn>>;


// Use

declare function otherFunction (
  irrelevantLabel: string,
  alsoNotUsed: number,
  onlyTheTypesMatter: boolean,
): void;

function example (objectParam: ParamsAsObject<typeof otherFunction, ['a', 'b', 'c']>) {
  // objectParam; // { a: string; b: number; c: boolean; }
  const {a, b, c} = objectParam;
  a; // string
  b; // number
  c; // boolean
}

jsejcksn
  • 27,667
  • 4
  • 38
  • 62
  • Is there a way to do this that retains info on whether the parameters were optional? `declare function otherFunction (irrelevantLabel: string, alsoNotUsed?: number): void;` – DaveJ May 26 '22 at 12:14
  • @DaveJ [Ask a new question](https://stackoverflow.com/questions/ask) – jsejcksn May 26 '22 at 19:28