Why does the key of [type] require a type? It may sound strange, but I am facing an issue.
Here is some example data:
export enum ENUM_Bike {
suzuki = 'suzuki',
yamaha = 'yamaha',
kawasaki = 'kawasaki'
}
export type TBike_StringMapping = { [key in ENUM_Bike]: string };
export const CONST_BikeIconName: Readonly<TBike_StringMapping> = {
suzuki : 'icon_a',
yamaha : 'icon_b',
kawasaki : 'icon_c',
}
export const CONST_BikeIconColor: Readonly<TBike_StringMapping> = {
suzuki : '#111',
yamaha : '#222',
kawasaki : '#333',
}
Implementation
<mat-icon
*ngFor="let item of CONST_BikeIconName | keyvalue: orderByJson"
[ngStyle]="{'background-color': CONST_BikeIconColor[item.key] }">
{{ item.value }}
</mat-icon>
I have been using this method, but now I encountered an issue
Element implicitly has an 'any' type because expression of type 'string' cannot be used to index type 'Readonly<TBike_StringMapping>'. No index signature with a parameter of type 'string' was found on type 'Readonly<TBike_StringMapping>'.ts(7053)
I came across a discussion, but it wasn't able to resolve my problem. I specifically need a type, not an interface. Can someone provide assistance? Thank you for your help.
TypeScript TS7015 error when accessing an enum using a string type parameter
Edit to Rachid O: There is a similar example and error here as well