【发布时间】:2020-10-29 12:07:57
【问题描述】:
这段代码运行良好,即通过基于字符串变量task的内容动态调用this.import_codeData(),在类DataIimporter上执行名为import_codeData的方法:
但是,在 Visual Studio Code 中,TypeScript 显示此错误:
如果发送一个简单的字符串,它会得到同样的错误:
虽然此代码运行良好,但如何让 TypeScript 不在编辑器中显示此错误?
附录:
我在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();
【问题讨论】:
-
typescriptlang.org/play 上的最小可重复示例会有所帮助
标签: reactjs typescript