ytxbnahn
V2EX  ›  问与答

一个 typescript 类型问题

  •  
  •   ytxbnahn · May 26, 2020 · 2288 views
    This topic created in 2357 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
    CAze
    May 26, 2020
    1
    a & {name?: string} 就好了
    ytxbnahn
    ytxbnahn
    OP
    May 26, 2020
    2
    @CAze 只是一个例子 现实情况有很多
    TarotVoyager
    TarotVoyager
    May 26, 2020
    3
    其实更困惑的是
    type a = { id?: number, phone?: string, name?: string }
    为什么不能是这个格式……
    horizon
    horizon
    May 26, 2020
    4
    interface A {
    id?: string;
    phone?: string;
    }

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

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