3

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 ?

5
  • 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]" Commented Feb 10, 2022 at 3: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). Commented Feb 10, 2022 at 3:34
  • If this meets your needs, I can make it into an answer. Commented Feb 10, 2022 at 3: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! Commented Feb 10, 2022 at 3:48
  • 1
    Confirmed: it's not possible Commented Feb 10, 2022 at 3:52

1 Answer 1

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
}

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to do this that retains info on whether the parameters were optional? declare function otherFunction (irrelevantLabel: string, alsoNotUsed?: number): void;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.