Perhaps what I'm asking to do is impossible, or I'm taking the wrong approach but is it possible to use a string to do a lookup on an imported namespace?
If I have a file: my_file.ts
and its contents are many of these:
export const MyThing: CustomType = {
propertyOne: "name",
propertyTwo: 2
}
export const AnotherThing: CustomType = {
propertyOne: "Another",
propertyTwo: 3
}
Then I have a file that is going to import all these, but needs to find and use 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]; // doesnt work
}
The error I get here 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 the literal string work but not a variable of type string?