admin管理员组文章数量:1310502
I have an object and I want to access the object value using a string index, but when I try to do that in TypeScript, I'm getting errors.
// Declared Globally
const Orientation = {
value: () => 0,
'next_value': () => someFunction.anotherFunction.sqrt(),
};
// _textValue type declared
_textValue : string;
// Declared inside constructor
this._textValue = 'next_value';
// value used inside a function
_getValue(){
const newValue = Orientation[this._textValue]();
}
And where I use Orientation[this._textValue]();
, I'm getting the error as:
Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ value: () => number; 'next_value': () => number; }'.ts(7053)
Any idea on how to resolve this?
I have an object and I want to access the object value using a string index, but when I try to do that in TypeScript, I'm getting errors.
// Declared Globally
const Orientation = {
value: () => 0,
'next_value': () => someFunction.anotherFunction.sqrt(),
};
// _textValue type declared
_textValue : string;
// Declared inside constructor
this._textValue = 'next_value';
// value used inside a function
_getValue(){
const newValue = Orientation[this._textValue]();
}
And where I use Orientation[this._textValue]();
, I'm getting the error as:
Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ value: () => number; 'next_value': () => number; }'.ts(7053)
Any idea on how to resolve this?
Share Improve this question edited Apr 29, 2020 at 5:49 Arjun V asked Apr 29, 2020 at 5:44 Arjun VArjun V 1975 silver badges16 bronze badges 6-
1
I think you can extend interface to be able to index using string like;
interface A { [b: string]: string }
You can look over to this playground – Dipesh Dulal Commented Apr 29, 2020 at 5:49 - Can you provide a reference? – Arjun V Commented Apr 29, 2020 at 5:57
- check here; typescriptlang/docs/handbook/interfaces.html and search for index signatures – Dipesh Dulal Commented Apr 29, 2020 at 5:57
- Does this answer your question? TypeScript Index Signature of object type implicitly has type any – Lauren Yim Commented Apr 29, 2020 at 5:59
-
1
Also, if you know
_textValue
will always be a key ofOrientation
you can use_textValue: keyof typeof Orientation
. – Lauren Yim Commented Apr 29, 2020 at 6:00
2 Answers
Reset to default 11main.ts
export {}
interface IDictionary {
[index: string]: string;
}
var params = {} as IDictionary;
//array with string index
params = {a: "Bhavesh",b: "Bond", c: "Urmil"};
//get array data using string index
var getData = params['b']; // output: Bond
var getData = params['B']; // output: undefined
console.log(getData);
I think your problem will be solved in the above example code. thank you..!
This is the first link google shows me for this problem. The easiest way i found is converting the object to any first:
const newValue = (Orientation as any)[this._textValue]();
And that's pretty much it.
本文标签: javascriptHow to access an object value in TypeScript using string indexStack Overflow
版权声明:本文标题:javascript - How to access an object value in TypeScript using string index - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741820843a2399353.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论