【问题标题】:Possible to use array values to create key-value type with corresponding string literals?可以使用数组值来创建具有相应字符串文字的键值类型吗?
【发布时间】:2020-12-09 08:10:07
【问题描述】:
//###  Desired Implementation & Outcome:  ###//

function objectFromArray<T extends string[]>(...arr:T){
    return {/* do things */} as ObjectFromArray<T>
}

const foo = objectFromArray("a", "b", "c")

// foo === {a:"a", b:"b", c:"c"}

// typeof foo.a === "a"
// typeof foo.b === "b"
// typeof foo.c === "c"


//###  Attempt 1:  ###//
type ObjectFromArray<T extends any[]> = {[K in T[number]]: T[number]}
// no error, but results in unions instead of single values:
//   typeof foo.a === ("a" | "b" | "c")
//   typeof foo.b === ("a" | "b" | "c")
//   typeof foo.c === ("a" | "b" | "c")


//###  Attempt 2:  ###//
type ObjectFromArray<T extends string[]> = {[K in T[number]]: T[number][K]}
// Error: Type 'K' cannot be used to index type 'T[number]'.ts(2536)

【问题讨论】:

    标签: typescript typescript-typings typescript-generics


    【解决方案1】:

    希望这就是你的意思:

    function objectFromArray<T extends string[]>(...arr:T){
        return {/* do things */} as ObjectFromArray<T>
    }
    
    type ObjectFromKeyList<KeyList extends string> = {[K in KeyList]: K}
    type ArrayToKeyList<T extends any[]> = T extends (infer P)[] ? P : never
    type ObjectFromArray<T extends any[]> = ObjectFromKeyList<ArrayToKeyList<T>>
    
    const foo = objectFromArray("a", "b", "c")
    
    foo.a // type is 'a'
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-01
      • 2015-11-20
      • 2018-12-12
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 2013-07-31
      相关资源
      最近更新 更多