react+typeScript框架config-overrides中设置Alias路径 报错的解决方法

该框架使用的是 customize-cra和react-app-rewired。

设置Alias路径:

const {
    override,
    addWebpackAlias
} = require('customize-cra');
const path = require('path')
module.exports = override(
    addWebpackAlias({
        ['@']: path.resolve(__dirname, './src'),
        ['@components']: path.resolve(__dirname, './src/components'),
        ['@utils']: path.resolve(__dirname, './src/utils'),
        ['@pages']: path.resolve(__dirname, './src/path')
    })
)

这属于正常的设置方法。然而在typescript中会报错。

解决方法:

1,在tsconfig.json的同级目录下建一个paths.json文件

paths.json:

{
    "compilerOptions": {
      "baseUrl": ".",
      "paths": {
        "@/*": ["src/*"],
        "@components/*": ["src/components/*"],
        "@pages/*": ["src/pages/*"],
        "@utils/*": ["src/utils/*"]
      }
    }
  }

2,更改tsconfig.json的文件配置

新增:"extends": "./paths.json" 

tsconfig.json:

{
  "extends": "./paths.json",
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

gitHub中问题的原issue:https://github.com/facebook/create-react-app/issues/5645

你可能感兴趣的:(React,TypeScript)