This code runs fine, i.e. executes the method called import_codeData
on the class DataIimporter
by dynamically calling this.import_codeData()
based on the content of the string variable task
:
However, in Visual Studio Code, TypeScript shows this error:
It gets the same error if a simple string is sent:
While this code works fine, how can I get TypeScript not to display this error in the editor?
ADDENDUM:
I reproduced this error online here at TypeScript Playground:
class DataImporter {
task: string;
constructor() {
this.task = "import_data001";
}
test() {
this[this.task]();
}
import_data001() {
console.log('importing data001');
}
}
const dataImporter = new DataImporter();
dataImporter.import_data001();
dataImporter.test();