Tuple To Object
介绍
将一个元组类型转换为对象类型,这个对象类型的键/值和元组中的元素对应。
例如:
ts
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as consttype 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 consttype result = TupleToObject<typeof tuple> // expected { 'tesla': 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
起点
ts
/* _____________ Your Code Here _____________ */typeTupleToObject <T extends readonly (string|number|symbol)[]> = {[P inT [number]]:P }consttuple = ['tesla', 'model 3', 'model X', 'model Y'] asconst ;consttupleNumber = [1, 2, 3, 4] asconst constsym1 =Symbol (1)constsym2 =Symbol (2)consttupleSymbol = [sym1 ,sym2 ] asconst consttupleMix = [1, '2', 3, '4',sym1 ] asconst typecases = [Expect <Equal <TupleToObject <typeoftuple >, { 'tesla': 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y' }>>,Expect <Equal <TupleToObject <typeoftupleNumber >, { 1: 1, 2: 2, 3: 3, 4: 4 }>>,Expect <Equal <TupleToObject <typeoftupleSymbol >, { [sym1 ]: typeofsym1 , [sym2 ]: typeofsym2 }>>,Expect <Equal <TupleToObject <typeoftupleMix >, { 1: 1, '2': '2', 3: 3, '4': '4', [sym1 ]: typeofsym1 }>>,]typeType '[[1, 2], {}]' does not satisfy the constraint 'readonly (string | number | symbol)[]'. Type '{} | [1, 2]' is not assignable to type 'string | number | symbol'. Type '{}' is not assignable to type 'string | number | symbol'.2344Type '[[1, 2], {}]' does not satisfy the constraint 'readonly (string | number | symbol)[]'. Type '{} | [1, 2]' is not assignable to type 'string | number | symbol'. Type '{}' is not assignable to type 'string | number | symbol'.error =TupleToObject <[[1, 2], {}]>
Take the Challengets
/* _____________ Your Code Here _____________ */typeTupleToObject <T extends readonly (string|number|symbol)[]> = {[P inT [number]]:P }consttuple = ['tesla', 'model 3', 'model X', 'model Y'] asconst ;consttupleNumber = [1, 2, 3, 4] asconst constsym1 =Symbol (1)constsym2 =Symbol (2)consttupleSymbol = [sym1 ,sym2 ] asconst consttupleMix = [1, '2', 3, '4',sym1 ] asconst typecases = [Expect <Equal <TupleToObject <typeoftuple >, { 'tesla': 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y' }>>,Expect <Equal <TupleToObject <typeoftupleNumber >, { 1: 1, 2: 2, 3: 3, 4: 4 }>>,Expect <Equal <TupleToObject <typeoftupleSymbol >, { [sym1 ]: typeofsym1 , [sym2 ]: typeofsym2 }>>,Expect <Equal <TupleToObject <typeoftupleMix >, { 1: 1, '2': '2', 3: 3, '4': '4', [sym1 ]: typeofsym1 }>>,]typeType '[[1, 2], {}]' does not satisfy the constraint 'readonly (string | number | symbol)[]'. Type '{} | [1, 2]' is not assignable to type 'string | number | symbol'. Type '{}' is not assignable to type 'string | number | symbol'.2344Type '[[1, 2], {}]' does not satisfy the constraint 'readonly (string | number | symbol)[]'. Type '{} | [1, 2]' is not assignable to type 'string | number | symbol'. Type '{}' is not assignable to type 'string | number | symbol'.error =TupleToObject <[[1, 2], {}]>
解决方案
Spoiler warning // Click to reveal answer
ts
// 实现1typeTupleToObject <T extends readonly (string|number|symbol)[]> = {[P inT [number]]:P }
ts
// 实现1typeTupleToObject <T extends readonly (string|number|symbol)[]> = {[P inT [number]]:P }
ts
// 实现2typeTupleToObject <T extends readonly any[]> = {[P inT [number]]:P }
ts
// 实现2typeTupleToObject <T extends readonly any[]> = {[P inT [number]]:P }
ts
// 实现3typeTupleToObject <T extends readonly (keyof any)[]> = {[P inT [number]]:P }
ts
// 实现3typeTupleToObject <T extends readonly (keyof any)[]> = {[P inT [number]]:P }
ts
// 实现4typeUnionToObj <U extends keyof any> = {[K inU ]:K }typeTupleToObject <T extends readonly any[]> =UnionToObj <T [number]>
ts
// 实现4typeUnionToObj <U extends keyof any> = {[K inU ]:K }typeTupleToObject <T extends readonly any[]> =UnionToObj <T [number]>
ts
// 实现5typeTupleToObject <T extends readonlyPropertyKey []> = {[K inT [number]]:K }
ts
// 实现5typeTupleToObject <T extends readonlyPropertyKey []> = {[K inT [number]]:K }
ts
// 实现6typeTupleToObject <T extends readonly (keyof any)[]> =T extends readonly (inferE )[]? { [K inE & keyof any]:K }: never
ts
// 实现6typeTupleToObject <T extends readonly (keyof any)[]> =T extends readonly (inferE )[]? { [K inE & keyof any]:K }: never
ts
// 实现7typeTupleToObject <T extends readonly (keyof any)[]> = {[K inT [number] asK ]:K }
ts
// 实现7typeTupleToObject <T extends readonly (keyof any)[]> = {[K inT [number] asK ]:K }