-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Open
Description
11 - Tuple To Object
TupleToObject is one of the more tricky challenges in the "easy" difficulty level. It teaches us how to remap tuples to other type structures, which is a skill that's going to come up quite a lot in many of the future challenges.
🎥 Video Explanation
🔢 Code
import type { Equal, Expect } from "./test-utils";
const tuple = [
"tesla",
"model 3",
"model X",
"model Y"
] as const;
type A1 = TupleToObject<typeof tuple>;
type B1 = {
tesla: "tesla";
"model 3": "model 3";
"model X": "model X";
"model Y": "model Y";
}
type C1 = Expect<Equal<A1, B1>>;
const tupleNumber = [1, 2, 3, 4] as const;
type A2 = TupleToObject<typeof tupleNumber>;
type B2 = { 1: 1; 2: 2; 3: 3; 4: 4 };
type C2 = Expect<Equal<A2, B2>>;
const tupleMix = [1, "2", 3, "4"] as const;
type A3 =TupleToObject<typeof tupleMix>;
type B3 = { 1: 1; "2": "2"; 3: 3; "4": "4" };
type C3 = Expect<Equal<A3, B3>>;
// @ts-expect-error(2344)
type E1 = TupleToObject<[[1, 2], {}]>;
// ============= Your Code Here =============
type TupleToObject<
T extends readonly PropertyKey[] // also any[]
> = {
[P in T[number]]: P;
};
type SameAsPropertyKey = keyof any;➕ More Solutions
For more video solutions to other challenges: see the umbrella list! #21338
Metadata
Metadata
Assignees
Labels
No labels