0
我有以下代码:在TypeScript中,如何使用索引类型查询运算符来获取类型属性的子集?
var o = { x: 5, y: 6, z: 'hi' }
type OnlyNumberProps = keyof typeof o; // 'x' | 'y' | 'z' I want it somehow to be 'x' | 'y'
var p : OnlyNumberProps = 'z' // How to get error here ?
var z : OnlyNumberProps = 'x' // And OK here ?
如何过滤类型OnlyNumberProps
只包含对象o
的数量属性的名称?
如果'x'和'z'都是'OnlyNumberProps'的元素,为什么你会期望在一种情况下出现错误而在另一种情况下没有错误?我没有得到你想要做的 – robkuz
我想要得到的类型OnlyNumberProps ='x'| 'y',即只具有那些属于o的属性,其类型为'number' –
@MadaraUchiha可能如何?你是基于现有问题吗? –