一个 typescript 类型问题

2020-05-26 17:18:58 +08:00
 ytxbnahn

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

type b = ?a

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

1070 次点击
所在节点    问与答
9 条回复
CAze
2020-05-26 17:21:12 +08:00
a & {name?: string} 就好了
ytxbnahn
2020-05-26 17:23:50 +08:00
@CAze 只是一个例子 现实情况有很多
chouchoui
2020-05-26 17:28:10 +08:00
其实更困惑的是
type a = { id?: number, phone?: string, name?: string }
为什么不能是这个格式……
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
2020-05-26 17:38:04 +08:00
我看错了,无视。。
chouchoui
2020-05-26 17:39:17 +08:00
ytxbnahn
2020-05-26 17:45:57 +08:00
@chouchoui 我的原有类型 是一个 多层的 object 我需要拿到他们的 key
chouchoui
2020-05-26 17:58:20 +08:00
@ytxbnahn #7 上面回复的 stackoverflow 链接应该能解决这个问题,如果用的 ts 2.8 的话
ytxbnahn
2020-05-26 18:09:36 +08:00
@chouchoui 感谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/675687

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX