Here is a funciton:
function factoryTableView(category: number) {
const views = {
1: new ImportExportTable()
};
return views[category];
}
I got this error message:
Element implicitly has an 'any' type because expression of type 'number' can't be used to index type '{ 1: ImportExportTable; }'.
No index signature with a parameter of type 'number' was found on type '{ 1: ImportExportTable; }'.
How to fix it?
CodePudding user response:
Objects implicitly have strings for their properties unless you specify otherwise.
Explicitly define a type for views
if you want to use numbers for the property names.
function factoryTableView(category: number) {
const views: Record<number, ImportExportTable> = {
1: new ImportExportTable()
};
return views[category];
}
(They will still be strings in JS, but TypeScript will require that you use numbers to access them and they will be converted to strings only at runtime).