I have a custom interface that I've written, and a variable that is typed using that interface.
I want to be able to index my object using a key string to return a specific value. And to do that I'm doing var[key]
, but I'm getting a typescript error.
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BasicMonitorSpec'. No index signature with a parameter of type 'string' was found on type 'BasicMonitorSpec'.
I've definitely done this before, so I'm not sure what I'm doing wrong.
export interface BasicMonitorSpec{
width: string,
eyeTech: boolean,
panelType: string,
refreshRate: string,
}
calculateWeight(inputSpec: BasicMonitorSpec, testSpec: BasicMonitorSpec){
let keys = Object.keys(testSpec);
keys.forEach(key => {
let inputValue: any;
inputValue = inputSpec[key]; //<-- Error is on this line
console.log("INPUTVALUE: ", inputValue);
});
}
My own working example
let results: PropResults = this.propResultService.propResults;
let summaryData: SummaryDataRow[] = [];
Object.keys(results).forEach(key => {
let mapping: PropMapping;
if(results[key] === '') return; //This works
}