When working with Typescript, the default behavior is to prevent certain actions unless it can verify that the string is a key of the type or the type includes an index signature.
For example, when using a known key:
class Foo {
selectedArray1 = [];
method(){
this['selectedArray1'] // permissible since result matches field type
const s = 'selectedArray1'; // allowed
this[s] // acceptable, as result matches field type
var someVariable = "selectedArray" + 1 // not verifiable as a key
this[someVariable] // prompts error
this[someVariable as keyof this] // assertion needed for acceptance, resulting in union of all field types of 'this'
}
}
Alternatively, utilizing an index signature:
class Foo {
[s: string] : any
selectedArray1 = [];
method(){
var someVariable = "selectedArray" + 1 // not verifiable as a key
this[someVariable] // permissible, as any type is valid
}
}