配置tsconfig paths后深层文件无法导入core模块的解决方法
解决深层文件无法导入@core模块的问题
1. 修正tsconfig的include配置
你的include字段仅包含了core/lib,但core/schema目录不在其中,TypeScript无法识别该目录下的文件类型声明。修改include,将整个core目录纳入范围:
"include": ["apps/", "index.ts", "core/**/*"],
2. 同步构建工具的别名配置(若使用)
TypeScript的paths配置仅用于类型检查,实际代码构建时(如Webpack、Vite、Rollup),需要单独配置别名映射,否则构建工具会无法找到模块:
- Webpack:在
webpack.config.js中添加resolve.alias
module.exports = { resolve: { alias: { '@core': path.resolve(__dirname, './core') } } }
- Vite:在
vite.config.ts中添加resolve.alias
import { defineConfig } from 'vite' import path from 'path' export default defineConfig({ resolve: { alias: { '@core': path.resolve(__dirname, './core') } } })
3. 重启TypeScript服务
修改tsconfig后,编辑器的TypeScript服务可能需要重启才能生效:
- 按
Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac) - 选择
TypeScript: Restart TS Server
完成以上步骤后,深层目录下的file1.tsx即可直接使用import { something } from '@core/schema/auth'导入根目录的core模块。
内容的提问来源于stack exchange,提问作者The Blind Hawk
相关产品推荐
相关产品推荐

