V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
ytxbnahn
V2EX  ›  问与答

一个 typescript 类型问题

  •  
  •   ytxbnahn · 2020-05-26 17:18:58 +08:00 · 1041 次点击
    这是一个创建于 1403 天前的主题,其中的信息可能已经有所发展或是发生改变。

    type a = {id?:number,phone?:string}|{id?:number,name?:string}

    type b = ?a

    期待: type b = 'id'|'phone'|'name'

    9 条回复    2020-05-26 18:09:36 +08:00
    CAze
        1
    CAze  
       2020-05-26 17:21:12 +08:00
    a & {name?: string} 就好了
    ytxbnahn
        2
    ytxbnahn  
    OP
       2020-05-26 17:23:50 +08:00
    @CAze 只是一个例子 现实情况有很多
    chouchoui
        3
    chouchoui  
       2020-05-26 17:28:10 +08:00
    其实更困惑的是
    type a = { id?: number, phone?: string, name?: string }
    为什么不能是这个格式……
    horizon
        4
    horizon  
       2020-05-26 17:37:05 +08:00
    interface A {
    id?: string;
    phone?: string;
    }

    interface B {
    id?: string;
    name?: string;
    }

    type C = keyof (A & B);
    horizon
        5
    horizon  
       2020-05-26 17:38:04 +08:00
    我看错了,无视。。
    ytxbnahn
        7
    ytxbnahn  
    OP
       2020-05-26 17:45:57 +08:00
    @chouchoui 我的原有类型 是一个 多层的 object 我需要拿到他们的 key
    chouchoui
        8
    chouchoui  
       2020-05-26 17:58:20 +08:00
    @ytxbnahn #7 上面回复的 stackoverflow 链接应该能解决这个问题,如果用的 ts 2.8 的话
    ytxbnahn
        9
    ytxbnahn  
    OP
       2020-05-26 18:09:36 +08:00
    @chouchoui 感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2842 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:57 · PVG 20:57 · LAX 05:57 · JFK 08:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.