Javascript 如何将动态导入与typescript一起使用?
我在做typescript,我想基于命令进行动态导入。我的设置没有问题Javascript 如何将动态导入与typescript一起使用?,javascript,typescript,ecmascript-6,Javascript,Typescript,Ecmascript 6,我在做typescript,我想基于命令进行动态导入。我的设置没有问题 static async GetCommand(command:string):Promise<(this: Discord.Message, ...args: string[]) => Promise<void>> { const f = (await import(`./classes/roles/everyone/commands/give_money/command`));
static async GetCommand(command:string):Promise<(this: Discord.Message, ...args: string[]) => Promise<void>> {
const f = (await import(`./classes/roles/everyone/commands/give_money/command`));
return f.default;
}
static async GetCommand(command:string):Promise-Promise>{
常量f=(等待导入(`./classes/roles/everyone/commands/give_money/command`);
返回f.default;
}
但这是硬编码到一个固定的进口位置。我想让它像这样充满活力
static async GetCommand(command:string):Promise<(this: Discord.Message, ...args: string[]) => Promise<void>> {
const f = (await import(`./classes/roles/everyone/commands/${command}/command`));
return f.default;
}
static async GetCommand(command:string):Promise-Promise>{
常量f=(等待导入(`./classes/roles/everyone/commands/${command}/command`);
返回f.default;
}
但随后它抱怨说f
属于any
类型,因此无法编译。我怎样才能解决这个问题
谢谢
然后它抱怨f是any类型,因此没有编译
如果有一个动态字符串作为import
TypeScript的参数,则假定有
我怎样才能解决这个问题
如果希望错误消失,请添加显式注释:
static async GetCommand(command:string):Promise<(this: Discord.Message, ...args: string[]) => Promise<void>> {
const f: any = (await import(`./classes/roles/everyone/commands/${command}/command`));
return f.default;
}
static async GetCommand(command:string):Promise-Promise>{
常量f:any=(等待导入(`./classes/roles/everyone/commands/${command}/command`);
返回f.default;
}
我得到了不安全的任意值赋值。。我也希望它消失。