V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cs419
V2EX  ›  TypeScript

求教 ts 的 字符串数组 泛型咋解析成对象的 key

  •  
  •   cs419 · 2021-12-03 14:52:48 +08:00 · 971 次点击
    这是一个创建于 846 天前的主题,其中的信息可能已经有所发展或是发生改变。
    function abc(...keys: string[]){
    }
    let {aa,bb,cc} = abc("aa","bb","cc")
    let {xx,yy,zz} = abc("xx","yy")
    

    abc 接受 可变参数 字符串
    返回值是 {"aa":person01, "bb":person02, "cc":person03 }
    希望代码 可以检查提示 let {xx,yy,zz} 中的 zz 有错

    对网上的博文 止步于

    type Pick<T, K extends keyof T> = {
      [P in K]: T[P]
    }
    

    实在不会写

    zbinlin
        1
    zbinlin  
       2021-12-07 20:01:05 +08:00   ❤️ 1
    ```
    declare function abc<K extends string>(...keys: K[]): { [k in K]: unknown };
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1046 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:09 · PVG 03:09 · LAX 12:09 · JFK 15:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.