升级依赖后NodeJS promisified glob报错:期望2个参数却传1个
解决glob与promisify冲突的类型错误
你的问题出在glob v10+ 已经原生支持Promise,不需要再用util.promisify包装。旧版本的glob是回调风格,升级后API重构,直接返回Promise,导致promisify处理后的函数签名与类型定义不匹配,触发参数数量错误。
修复步骤:
- 删除
promisify的导入以及globPromise的定义 - 直接调用
glob函数并使用await
修改后的完整代码:
import { glob } from 'glob'; const slashCommandFiles = await glob( `${__dirname}/../Commands/Slash/*/*{.ts,.js}` ); slashCommandFiles.forEach(async (filePath) => { const command: CommandType = await this.importFile(filePath); if (!command.name) return; this.commands.set(command.name, command); commands.push(command); });
补充说明:
glob v10.x 对核心API做了重大调整,默认导出的glob函数不再接受回调参数,而是直接返回Promise。继续用promisify包装会让TypeScript认为你要传入回调风格的函数参数,从而触发"Expected 2 arguments"的类型错误。
内容的提问来源于stack exchange,提问作者123253
相关产品推荐
相关产品推荐

