ytxbnahn
V2EX  ›  问与答

一个 typescript 类型问题

  •  
  •   ytxbnahn · May 26, 2020 · 1885 views
    This topic created in 2184 days ago, the information mentioned may be changed or developed.

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

    type b = ?a

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

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

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

    type C = keyof (A & B);
    horizon
        5
    horizon  
       May 26, 2020
    我看错了,无视。。
    ytxbnahn
        7
    ytxbnahn  
    OP
       May 26, 2020
    @chouchoui 我的原有类型 是一个 多层的 object 我需要拿到他们的 key
    TarotVoyager
        8
    TarotVoyager  
       May 26, 2020
    @ytxbnahn #7 上面回复的 stackoverflow 链接应该能解决这个问题,如果用的 ts 2.8 的话
    ytxbnahn
        9
    ytxbnahn  
    OP
       May 26, 2020
    @chouchoui 感谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5702 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 03:23 · PVG 11:23 · LAX 20:23 · JFK 23:23
    ♥ Do have faith in what you're doing.