Manually mapping tuple to object
Tuple indexes can be easily mapped by using object keys.
Syntax:
type Type = [Type1, Type2];
// Object with Tuple
const objWithTuple: Record<string, Type> = {
k1: [v1, v2],
k2: [v3, v4],
};
Example: The below code will map the tuple to an JavaScript object.
type Point = [number, number];
const pointObj: Record<string, Point> =
{
o: [0, 0],
e: [10, 20],
};
console.log(pointObj);
Output:
{
"o": [0, 0],
"e": [10, 20]
}
How to map Enum/Tuple to Object in TypeScript ?
Mapping enum or tuple values to objects is a usual action in TypeScript when working with different data representations. Enumerations (enum) and Tuples are two types of data that can be mapped into objects differently based on the methods below in TypeScript.
Table of Content
- Manually mapping Enum to Object
- Manually mapping tuple to object
- Using Object.fromEntries() with Enum
- Mapping Tuple to Object with Reduce
Contact Us