Tìm kiếm trang web

reactjs - typeError: Không thể gán cho thuộc tính chỉ đọc 'paths' của đối tượng trong compilerOptions trong 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"
     ]
  }

Nhưng nhận được lỗi như dưới đây:

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)

Có phải vì quyền thư mục hoặc thiếu gì đó trong cấu hình không

Câu trả lời #1

Rõ ràng là có một lỗi trong [email .

Giải pháp tạm thời đã giúp tôi là:

Hạ cấp react-scripts:

yarn add [email 

Tạo một paths.json trong thư mục gốc của dự án:

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

Thêm một extends vào 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"
  ]
}

Câu trả lời #2

Theo vấn đề trên GitHub, nếu bạn chỉ xóa tệp tsconfig.json rồi chạy "npm start" thì nó sẽ tạo lại tệp tsconfig.json cho bạn và mọi thứ sẽ hoạt động bình thường.

Bài viết liên quan