Search code examples
reactjstypescripttsconfig

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"
     ]
  }

But getting error as below:

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)

Is this because of the folder permission or anything missing in the configuration


Solution

  • Apparently, there's a bug in Create-React-App@4.0.0.

    The workaround that solved for me was:

    Downgrade react-scripts:

    yarn add react-scripts@3.4.4
    

    Create a paths.json in the project's root:

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

    Add an extends to 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"
      ]
    }