【问题标题】:typeError: Cannot assign to read only property 'paths' of object for compilerOptions in tsconfigtypeError:无法分配给 tsconfig 中 compilerOptions 对象的只读属性“路径”
【发布时间】:2020-10-29 14:38:54
【问题描述】:
**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)

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

【问题讨论】:

    标签: reactjs typescript tsconfig


    【解决方案1】:

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

    为我解决的解决方法是:

    降级react-scripts:

    yarn add react-scripts@3.4.4
    

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

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

    extends 添加到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,
        "noFallthroughCasesInSwitch": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react"
      },
      "include": [
        "src"
      ]
    }
    

    【讨论】:

    • 谢谢,浪费了这么多时间
    • 您为我节省了很多时间,谢谢。赞成。但他们应该解决这个问题
    • 似乎在 4.0.1 版中已修复
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      具体来说,升级后中断的设置是 compileOnSave,不再需要指定。

      【讨论】:

      • 谢谢@DrShaffopolis!最近我已将"react-scripts" 升级为"^4.0.1",并且按照上面的建议进行更改对我有用。 +1
      猜你喜欢
      • 2023-03-13
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 2017-11-30
      • 2021-01-08
      相关资源
      最近更新 更多