Tuple to Object
#
introductionGive an array, transform into an object type and the key/value must in the given array.
For example
ts
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const;const result: TupleToObject<typeof tuple>; // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
ts
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const;const result: TupleToObject<typeof tuple>; // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
#
start pointtsTry
/* _____________ Your Code Here _____________ */typeTupleToObject <T extends readonly any[]> = any;Â/* _____________ Test Cases _____________ */consttuple = ['tesla', 'model 3', 'model X', 'model Y'] as const;Âtypecases = [Expect <Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Equal <TupleToObject <typeoftuple >,{tesla : 'tesla'; 'model 3': 'model 3'; 'model X': 'model X'; 'model Y': 'model Y' }>>];
Take the ChallengetsTry
/* _____________ Your Code Here _____________ */typeTupleToObject <T extends readonly any[]> = any;Â/* _____________ Test Cases _____________ */consttuple = ['tesla', 'model 3', 'model X', 'model Y'] as const;Âtypecases = [Expect <Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Equal <TupleToObject <typeoftuple >,{tesla : 'tesla'; 'model 3': 'model 3'; 'model X': 'model X'; 'model Y': 'model Y' }>>];