Surely this is rudimentary TS, but I can't find the syntax.
I have a type:
type MyType = {
prop1: string;
prop2: string;
}
Now inside a function I have a variable of type MyType, but I need to dynamically get the value of a particular property on it, like:
const myMethod = (typeX: MyType, num: number) => {
const property1 = typeX['prop${num}`]; // problem line!
}
The typescript error I'm seeing 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 fine.
How do I cast that string as a property on MyType?
typeX['prop1']makes it statically analyzable to the compiler, and it's recognizing the property as being valid.typeX['invalid property']?((any)typeX)['prop${num}']?'any' only refers to a type, but is being used as a value herewith that.