๐Ÿ’™ Type Challenges/type-challenges

[TS] type-challenges : 11. Tuple to Object

์„ ๋‹ฌ 2023. 7. 3. 15:22
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

๋ฐฐ์—ด(ํŠœํ”Œ)์„ ๋ฐ›์•„, ๊ฐ ์›์†Œ์˜ ๊ฐ’์„ key/value๋กœ ๊ฐ–๋Š” ์˜ค๋ธŒ์ ํŠธ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž…์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

 

์˜ˆ์‹œ

const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const

type result = TupleToObject<typeof tuple> // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}

 

ํ’€์ด

๊ฐ์ฒด์˜ ํ‚ค๋„, ๊ฐ’๋„ T๋ฐฐ์—ด์˜ ์š”์†Œ๊ธฐ ๋•Œ๋ฌธ์— ํ‚ค๊ฐ’ kv๊ฐ€ T[number]์— ์†ํ•ด์žˆ๊ณ  ์ด ๊ฒƒ์ด ํ‚ค์ธ ๋™์‹œ์— ๊ฐ’์ด๋ผ๊ณ  ๋ช…์‹œํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ๋œ๋‹ค.

์ด๋•Œ T๋Š” string ๋˜๋Š” number ๊ฐ€ ํฌํ•จ๋œ ๋ฐฐ์—ด์ด๋ผ๋Š” ์ ๋„ ๋ช…์‹œํ•ด์ค€๋‹ค.

type TupleToObject<T extends readonly (string|number)[]> = {
  [kv in T[number]] : kv; 
}

 

์ถœ์ฒ˜

https://github.com/type-challenges/type-challenges/blob/main/questions/00011-easy-tuple-to-object/README.md

 

GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge

Collection of TypeScript type challenges with online judge - GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge

github.com

 

๋ฐ˜์‘ํ˜•

'๐Ÿ’™ Type Challenges > type-challenges' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TS] type-challenges: 18. Length of Tuple  (0) 2023.07.05
[TS] type-challenges: 14. First of Array  (0) 2023.07.04
[TS] type-challenges : 7. Readonly  (0) 2023.06.28
[TS] type-challenges : 4. Pick  (0) 2023.06.28
[TS] type-challenges : 13. Hello World  (0) 2023.06.28