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 of Orientation you can use _textValue: keyof typeof Orientation. – Lauren Yim Commented Apr 29, 2020 at 6:00
 |  Show 1 more ment

2 Answers 2

Reset to default 11

main.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