Although this may seem like a simple TS issue, I'm struggling to find the correct syntax.
Consider this type:
type MyType = {
prop1: string;
prop2: string;
}
Now, within a function, I have a variable of type MyType. However, I need to dynamically retrieve the value of a specific property from it, like so:
const myMethod = (typeX: MyType, num: number) => {
const property1 = typeX['prop${num}`]; // This is where the issue arises!
}
The TypeScript error I encounter is:
TS7053 Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'MyType'. No index signature with a parameter of type 'string' was found on type 'MyType'
Changing it to typeX['prop1']
works without errors.
How do I properly convert that string into a valid property of MyType?