簡體   English   中英

Typescript 構造函數參數。 解析錯誤:意外的保留字“公共”

[英]Typescript Constructor Parameters. Parsing error: Unexpected reserved word 'public'

我想在 typescriptp + react 中創建 class 我決定使用Typescript 構造函數參數但是當我這樣做時在編譯過程中出現錯誤。

那是我的用戶數據 class

enum SecurityRole {
    USER = "USER",
    ADMIN= "ADMIN"
}
export class UserData {
    constructor(public name: string, readonly securityRole = SecurityRole.USER) { }
}
export const createUserDataFromJson = (json: any): UserData => {
    console.log(`user from json : ` + json)
    return new UserData(json.name, json.securityRole)
}

但我收到一個錯誤:

  Line 8:17:  Parsing error: Unexpected reserved word 'public'

   6 | }
   7 | export class UserData {
>  8 |     constructor(public name: string, readonly securityRole = SecurityRole.USER) { }
     |                 ^
   9 | }
  10 | export const createUserDataFromJson = (json: any): UserData => {
  11 |     console.log(`user from json : ` + json)

我認為它可能與 tsconfig.json 相關,所以這里是:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "esnext",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": [
    "src"
  ]
}

我正在使用npm run build這是我的項目中react-scripts build (4.0.2)

npm 版本6.14.6節點版本v12.18.3

我在那里做錯什么了嗎? 還是與我的項目設置有關?

我設法通過將 eslintConfig 添加到我的 package.json 來解決這個問題

 "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM