1

Suppose I have a tuple type like this:

type Test<T extends string, V> = { type: T, value: V };
type Tuple = [
    Test<'string', string>, 
    Test<'number', number>, 
    Test<'integer', number>
];

From the type Tuple I would like to generate the type

type TupleObject = {
    string: string,
    number: number,
    integer: number,
};

Is this at all possible and if so, how? So far I have only managed to get

type TupleObject = {
    string: string | number,
    number: string | number,
    integer: string | number,
};

1 Answer 1

2

You can use a mapped type with an as clause and map over Tuple[number]

type TupleToObject<T extends Array<Test<string, any>>> = {
    [K in T[number] as K['type']]: K['value']
}

Playground Link

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

Comments

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.