Is it feasible to utilize a string for performing a lookup on an imported namespace, or am I approaching this the wrong way?
Consider a file named my_file.ts
with contents similar to:
export const MyThing: CustomType = {
propertyOne: "name",
propertyTwo: 2
}
export const AnotherThing: CustomType = {
propertyOne: "Another",
propertyTwo: 3
}
Now, if there is another file that imports these and needs to access them dynamically based on a string:
import * as allthings from "dir/folder/my_file"
function doStuff() {
let currentThing = allthings['MyThing']; // works
let name = 'MyThing';
let currentThing2 = allthings[name]; // doesn't work
}
The error encountered is:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof import("dir/folder/my_file")'. No index signature with a parameter of type 'string' was found on type 'typeof import("dir/folder/my_file")'
Why does a literal string work but not a variable of type string in this scenario?