0

In TypeScript, we can use a Tuple of types to create typed named rest arguments. This is useful for rest arguments.

type Params = [myString: string, myNumber: number, mySecondString: string]

const fn = (...args: Params) => null

// Type is (myString: string, myNumber: number, mySecondString: string) => null

Is there any way to achieve the same when using a mapped tuple type?

type TypeMap = {
    "string": string
    "number": number
}

const inputs = [
    {name: "myString", type: "string"},
    {name: "myNumber", type: "number"},
    {name: "mySecondString", type: "string"},
] as const

type InputToTuple<T extends typeof inputs> = { [P in keyof T]: TypeMap[T[P]["type"]] };

type Params = InputToTuple<typeof inputs>

const fn = (...args: ParamsIWant) => null
// Type is (args_0: string, args_1: number, args_2: string) => null

Is there any way to supply the names of these parameters when the Tuple is created as a mapped type?

The core is to make this line:

type InputToTuple<T extends typeof inputs> = { [P in keyof T]: TypeMap[T[P]["type"]] };

Produce this result:

type Params = InputToTuple<typeof inputs>
// Type is [myString: string, myNumber: number, mySecondString: string]

Is this possible at all?

Playground


A note about why: I am building a way to let TypeScript infer types based on an Ethereum JSON ABI (and using as const to get type narrowing). This works perfectly for everything except named parameters.

2
  • 1
    I authored issue #44939 describes this
    – mochaccino
    Commented Apr 13, 2022 at 18:21
  • 1
    Sadly, the only way to declare the name of a tuple is explicitly and literally. And those can get lost when you start mapping over them, and you definitely cannot dynamically name them with string literal types. So as far as Typescript 4.6 is concerned, I think this is impossible.
    – Alex Wayne
    Commented Apr 13, 2022 at 18:27

1 Answer 1

0

To the question "Is mapped named tuple members possible in TypeScript?" the answer is "yes", with the tuple rest syntax.

The idea is to manipulate the labels and the values separately.

In your case, there is the additional constraint of not knowing in advance the labels. That is not possible, you would have to build a list of all possible labels.

type Labels = {
    myBool: [myBool: unknown],
    myString: [myString: unknown],
    myNumber: [myNumber: unknown],
    mySecondString: [mySecondString: unknown]
};

type TypeMap = {
    "string": string
    "number": number
}

const inputs = [
    {name: "myString", type: "string"},
    {name: "myNumber", type: "number"},
    {name: "mySecondString", type: "string"},
] as const

type Result = GetTupleFromInput<Labels, TypeMap, typeof inputs>;
// Result: [myString: string, myNumber: number, mySecondString: string];

playground

The magic happens here

// We stitch together the tuple template
type TemplateFromInput<
    Labels extends {[k: string]: [unknown]},
    Input extends readonly { name: keyof Labels }[],
    I extends number = 0,
    R extends unknown[] = []
> = I extends Input['length'] ? R
    : TemplateFromInput<Labels, Input, Next<I>, [...R, ...Labels[Input[I]['name']]]>

type Next<I extends number> = [1,2,3,4,5,6,7,8,9][I];

// We extract the values / I generalised InputToTuple
type ValuesFromInput<T, TypeMap> = {
    [P in keyof T]:
        T[P] extends { type: keyof TypeMap }
        ? TypeMap[T[P]["type"]] : T[P]
};

// We inject the values back in the template
type GetTupleFromInput<
    Labels extends {[k: string]: [unknown]},
    TypeMap extends {[k: string]: unknown},
    Input extends readonly { type: keyof TypeMap, name: keyof Labels }[],
    Template extends unknown[] = TemplateFromInput<Labels, Input>,
    Values = ValuesFromInput<Input, TypeMap>
> = {[K in keyof Template]: Values[K & keyof Values]};

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.