reactjs - 不得设置 compilerOptions.paths(不支持别名导入)

标签 reactjs typescript tsconfig tsconfig-paths

我正在尝试映射 tsconfig.json 中的路径以摆脱 相对路径 hell 。我的 React App 基于 Create-React-App .
我试过这个 SO thread并在我的 tsconfig.json 中添加了路径。我的 tsconfig.json 是

{
  "compilerOptions": {
    "baseUrl": "src",
    "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",
    "strictNullChecks": false
  },
  "include": [
    "src"
  ]
}

当我在 VS Code 中编译我的项目时,它会从 tsconfig.json 中删除路径条目,并显示以下消息。为什么我的 不支持别名导入 react 脚本 基于 React 项目?
enter image description here

最佳答案

不再支持路径别名
你现在可以这样做,直接导入相对于 src 的文件目录
转到您的 jsconfig.json文件添加基本 URL 为 "."

"compilerOptions": {
    "baseUrl":".",
    ...
那么你可以直接从 src 导入东西目录
import Myfile from "src/myfile.js"

关于reactjs - 不得设置 compilerOptions.paths(不支持别名导入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64172923/

相关文章:

reactjs - 在 createTheme() 中添加自定义主题变量

javascript - Angular 过滤阵列

javascript - NullInjectorError : No provider for Overlay! NgxToastr

node.js - node_modules/@types/node/index.d.ts(20,1): error TS1084: Invalid 'reference' directive syntax

angular - 将 aws-sdk 集成到 angular2 得到 "Cannot find module ' 流'"

typescript npm 包 - 如何不必从 "dist/"导入

javascript - typescript 路径无法解析

javascript - Redux Web 扩展 - 未捕获的类型错误 : Invalid attempt to spread non-iterable instance

javascript - 使用 Jest + react-testing-library 测试异步 `componentDidMount()`

javascript - 如何将属性从有状态组件传递到包装子组件的 HOC 中使用的事件处理程序?