代码之家  ›  专栏  ›  技术社区  ›  Mithun Shreevatsa

typeError:无法为tsconfig中compilerOptions的对象分配只读属性“paths”

  •  0
  • Mithun Shreevatsa  · 技术社区  · 5 年前
    **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)
    

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

    0 回复  |  直到 5 年前
        1
  •  10
  •   Guria    5 年前

    显然,有一个 bug inCreate-React-App@4.0.0.

    为我解决的解决方法是:

    降级 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"
      ]
    }
    
        2
  •  2
  •   Ryan Kennel    5 年前

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

        3
  •  1
  •   DrShaffopolis    5 年前

    我用以下更改修复了它:

    如前所述,删除tsconfig并让它自动创建默认配置也可以解决这个问题。不过,如果你和我一样,你可能已经担心了一秒钟,你只能使用默认配置,而解决方法是打破绝对导入的基本路径。(并非如此——呼!)

    具体来说,我在升级后破坏的设置是compileOnSave,不再需要指定。

    enter image description here