如何在不使用“字符串”索引的情况下将对象值转换为tumpecript联合?
// from
const obj = {
a: {
aa: 'A',
cc: 'C',
},
b: {
bb: 'B',
cc: 'C',
}
} as const;
// to
type ObjValue = 'A' | 'B' | 'C';
嗨,我想从对象获得联合类型。
因此,我尝试使用typeof obj [keyOf obj typeof obj]
模式,但它只是返回。
type ObjValue = 'C'
如何在没有字符串
索引的情况下仅组成对象的字符串值?
// from
const obj = {
a: {
aa: 'A',
cc: 'C',
},
b: {
bb: 'B',
cc: 'C',
}
} as const;
// to
type ObjValue = 'A' | 'B' | 'C';
Hi, I want to get union type from object.
So I tried with typeof obj[keyof typeof obj]
pattern, but it just returns this.
type ObjValue = 'C'
How do I get the union type composed only string values of object without String
indexing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么
obj类型[keyOf obj typeof obj]
模式实际上无法与工会合作。不过,使用映射类型可以解决问题。I am not sure why the
typeof obj[keyof typeof obj]
pattern does not really work with unions. Using mapped types solves the problem though.Playground