π Typescript/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;
}
μΆμ²
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
λ°μν