类型错误:无法为tsconfig中的compilerOptions的只读属性'paths'分配值。

6
**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)

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

3个回答

10

显然,Create-React-App@4.0.0存在错误

对我有效的解决方法是:

降级react-scripts

yarn add react-scripts@3.4.4
在项目的根目录下创建一个paths.json
{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "@Components/*": ["components/*"]
    }
  }
}
tsconfig.json 中添加 extends
{
  "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"
  ]
}

谢谢,这个问题浪费了我很多时间。 - Drowned
你节省了我很多时间,谢谢。已点赞。但他们应该修复这个问题。 - Maccurt
2
似乎在4.0.1版本中已经修复。 - Juan Angel

2

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


1
我用以下更改修复了它:
正如先前提到的,删除您的tsconfig并让它自动创建一个默认的也可以解决这个问题。如果你像我一样,可能会惊慌一秒钟,认为你只能使用默认配置,而解决方法会破坏你的绝对导入的基本路径。(不是这样-哎呀!)
具体来说,我遇到的问题是编译保存,现在不再需要指定。

enter image description here


谢谢@DrShaffopolis!! 最近我已经将"react-scripts"升级到"^4.0.1",并且按照上面提出的更改对我有用。+1 - jherax

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接