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可能如何?你是基於現有問題嗎? –