typeError: Cannot assign to read only property 'paths' of object for compilerOptions in tsconfig

typeError: Cannot assign to read only property 'paths' of object for compilerOptions in tsconfig

**Trying to set the path inside tsconfig.json for compiler to treat src as baseUrl in react typescript project**

  {
  "compilerOptions": {
    "target": "es5",
    "baseUrl": "src",
    "paths": {
      "*": ["src/*"]
    }
    "include": [
       "src"
     ]
  }

但是报错如下:

TypeError: Cannot assign to read only property 'paths' of object '#<Object>'
    at verifyTypeScriptSetup (/Users/apple/Documents/projects/my-app/node_modules/react-scripts/scripts/utils/verifyTypeScriptSetup.js:239:43)

这是因为文件夹权限还是配置中缺少任何内容

显然,Create-React-App@4.0.0 中有一个 bug

为我解决的解决方法是:

降级react-scripts:

yarn add react-scripts@3.4.4

在项目的根目录中创建一个paths.json

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

添加一个extendstsconfig.json:

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

根据 GitHub 问题,如果您只是删除 tsconfig.json 文件然后 运行 "npm start" 它将为您重新创建 tsconfig.json 文件,一切都会工作正常。

我通过以下更改修复了它:

如前所述,删除您的 tsconfig 并让它自动创建默认配置也可以解决此问题。但是,如果您像我一样,您可能已经惊慌了一秒钟,因为您只能使用默认配置,解决方法会破坏绝对导入的基本路径。 (不是这样 - 呸!)

具体来说,我的设置破坏了 post-upgrade 是 compileOnSave,不再需要指定。