我有一個對象,我想使用字符串索引訪問對象值,但是當(dāng)我嘗試在 TypeScript 中執(zhí)行此操作時,我遇到了錯誤。// Declared Globallyconst Orientation = { value: () => 0, 'next_value': () => someFunction.anotherFunction.sqrt(),};// _textValue type declared _textValue : string;// Declared inside constructorthis._textValue = 'next_value';// value used inside a function_getValue(){ const newValue = Orientation[this._textValue](); }在我使用的地方Orientation[this._textValue]();,我得到的錯誤是:元素隱式具有“any”類型,因為“any”類型的表達(dá)式不能用于索引類型“{ value: () => number; 'next_value': () => 數(shù)字;}'.ts(7053)關(guān)于如何解決這個問題的任何想法?
如何使用字符串索引訪問 TypeScript 中的對象值
慕桂英546537
2022-07-21 21:30:29