使用Vite Glob Import设置选项报错:eager不在GlobOptions类型中
解决Vite Glob Import中
eager/as选项的TypeScript类型错误 问题原因
你遇到的类型错误本质是Vite版本不兼容或类型定义未正确加载:
eager和as这类Glob导入选项是Vite 2.7及以上版本才支持的特性,若你的Vite版本低于2.7,类型定义里自然没有这些属性。- 若项目中错误安装了第三方
@types/vite包,它的类型定义可能过时,会覆盖Vite官方自带的正确类型。
解决方案
- 升级Vite到最新稳定版:
执行命令:npm install vite@latest # 或使用yarn yarn add vite@latest - 卸载过时的第三方类型包:
若之前装过@types/vite,立即卸载:npm uninstall @types/vite # yarn版本 yarn remove @types/vite - 检查TypeScript配置:
确保tsconfig.json的compilerOptions.types中包含vite,且未通过exclude排除Vite的类型文件:{ "compilerOptions": { "types": ["vite"] } } - 同步MDX插件版本:
若使用@vitejs/plugin-mdx,确保它的版本与Vite兼容,建议也升级到最新版:npm install @vitejs/plugin-mdx@latest
验证
修改后重启TypeScript语言服务(比如VSCode中按Ctrl+Shift+P选择"TypeScript: Restart TS Server"),重新构建即可解决类型错误。
内容的提问来源于stack exchange,提问作者matt
相关产品推荐
相关产品推荐

