Im pretty new to Typescript, and have hit an error I dont quite know how to fix.
Im getting the error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ 'lf-text': typeof AddonLfTextModel; 'lf-image': typeof AddonLfImageModel; gameflow: typeof AddonGameflowModel; }'.
No index signature with a parameter of type 'string' was found on type '{ 'lf-text': typeof AddonLfTextModel; 'lf-image': typeof AddonLfImageModel; gameflow: typeof AddonGameflowModel; }'.
Im not quite sure on how to solve this TS compile error??
In my class I have these types and interfaces:
export interface ContentAddon {
id: string;
alias: string;
model: ContentAddonType;
}
export type ContentAddonType =
| AddonLfTextModel
| AddonLfImageModel
| AddonGameflowModel;
const AddonMapping = {
'lf-text': AddonLfTextModel,
'lf-image': AddonLfImageModel,
gameflow: AddonGameflowModel
};
interface ColumnModelState {
size: number;
addons: ContentAddon[];
}
In my code (In a method), im trying to to the following, but getting the error: (The _state is outside the method, but in a class)
private _state?: ColumnModelState;
parse(data: ContentColumnData): void {
console.log('Column data', data);
// TODO: Parse data
const state = this.state;
state.size = data.size;
state.addons = data.addons.map((addon: ContentAddon) => {
//IM GETTING AN ERROR HERE: 'string' can't be used to index type
if (typeof AddonMapping[addon.alias] === 'undefined') {
throw new Error(`Unrecognized addon "${addon.alias}"`);
}
return {
id: generateUniqueId(),
alias: addon.alias,
model: new AddonMapping[addon.alias](addon) //GETTING A TS ERROR HERE: 'string' can't be used to index type
};
});
}