【发布时间】:2021-03-20 19:48:51
【问题描述】:
我想从之前定义的常量中获取属性的类型。
const my_constant = {
user: {
props: {
name: {
type: 'string'
}
}
},
media: {
props: {
id: {
type: 'number'
}
}
}
} as const;
type Name = keyof typeof my_constant;
type Constructed<T extends Name> = {
[K in keyof typeof my_constant[T]['props']]: typeof my_constant[T]['props'][K]['type']
// ~~~ Type '"type"' cannot be used to index type ...
}
我不明白为什么我不能使用“type”作为索引,但我可以使用“props”。
如果 typescript 可以推断出始终存在“props”属性,为什么它不能推断出始终存在“type”?
还有其他方法可以获取类型吗?
我想要实现的是这样的:
const user:Constructed<'user'> = {
name: 'John'
}
const media:Constructed<'media'> = {
id: 123
}
const user2:Constructed<'user'> = {
name: 444
// ~~~ Error
}
const media2:Constructed<'media'> = {
id: 'something'
// ~~~ Error
}
这是带有确切错误的游乐场链接:
【问题讨论】:
-
我不确定这是否真的可行。运行时不存在输入(在其中运行
const)。 -
如果您点击操场链接,您可以看到我可以轻松拥有“用户”和“媒体”类型,也可以使用“道具”对象类型。我不明白为什么我不能再往下一层。
-
我还认为,您想要实现的是,该 id 具有打字稿类型“数字”。您正在分配一个类型文字。 playground
标签: typescript type-inference typescript-generics typeof