我通过将 to 更改为Angelo P 的回答工作config-overrides.js
const path = require('path');
module.exports = function override(config, env) {
config.resolve = {
...config.resolve,
alias: {
...config.resolve.alias,
"@src": path.resolve(__dirname, 'src/'),
"@api": path.resolve(__dirname, "src/API/"),
"@assets": path.resolve(__dirname, "src/assets/"),
"@components": path.resolve(__dirname, "src/components/"),
"@containers": path.resolve(__dirname, "src/containers/"),
"@css": path.resolve(__dirname, "src/css/"),
"@customHooks": path.resolve(__dirname, "src/CustomHooks/"),
"@helperFuncs": path.resolve(__dirname, "src/HelperFuncs/"),
"@hoc": path.resolve(__dirname, "src/hoc/"),
"@middlewares": path.resolve(__dirname, "src/middlewares/"),
"@models": path.resolve(__dirname, "src/models/"),
"@store": path.resolve(__dirname, "src/store/"),
"@actions": path.resolve(__dirname, "src/store/actions"),
"@reducers": path.resolve(__dirname, "src/store/reducers/"),
"@sagas": path.resolve(__dirname, "src/store/sagas/"),
"@typings": path.resolve(__dirname, "src/Typings/"),
"@utils": path.resolve(__dirname, "src/utils/")
},
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json', '.d.ts']
};
return config;
};
像魅力一样工作,但唯一缺少的是 VSCode 自动补全。React 启动时会出现以下警告:
The following changes are being made to your tsconfig.json file:
- compilerOptions.paths must not be set (aliased imports are not supported)
所以我假设它path
从 tsconfig.json 中删除了该字段,并且 VSCode 无法获取别名,因此ESLint
并TS
在 VSCode 中给出以下错误,但一切正常。
Unable to resolve path to module '@typings/...'.eslintimport/no-unresolved
Cannot find module '@typings/...' or its corresponding type declarations.ts(2307)
有人对此有解决方案吗?
更新
通过进行以下更改,设法使其全部正常工作:
tsconfig.paths.json
{
"compilerOptions": {
"paths": {
"@src/*": ["./src/*"],
"@api/*": ["./src/API/*"],
"@assets/*": ["./src/assets/*"],
"@components/*": ["./src/components/*"],
"@containers/*": ["./src/containers/*"],
"@css/*": ["./src/css/*"],
"@customHooks/*": ["./src/CustomHooks/*"],
"@helperFuncs/*": ["./src/HelperFuncs/*"],
"@hoc/*": ["./src/hoc/*"],
"@middlewares/*": ["./src/middlewares/*"],
"@models/*": ["./src/models/*"],
"@store/*": ["./src/store/*"],
"@actions/*": ["./src/store/actions*"],
"@reducers/*": ["./src/store/reducers/*"],
"@sagas/*": ["./src/store/sagas/*"],
"@typings/*": ["./src/Typings/*"],
"@utils/*": ["./src/utils/*"]
}
}
}
注意*
末尾的@.../*
tsconfig.json
{
"extends": "./tsconfig.paths.json",
"compilerOptions": {
"target": "es6",
"lib": [
"dom",
"dom.iterable",
"esnext",
"es2015.promise"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"plugins": [
{
"name": "typescript-plugin-css-modules"
}
],
"resolveJsonModule": true,
"baseUrl": ".",
},
"include": [
"src",
"src/**/*.ts"
]
}
npm i -D eslint-plugin-import @typescript-eslint/parser eslint-import-resolver-typescript
.eslintrc.json
{
...,
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx", ".d.ts"]
},
"typescript": {}
}
}
}