Typescript Contructor Parameters. Parsing error: Unexpected reserved word 'public'

I wanted to create class in typescritp + react
I decided to use Typescript constructor parameters
But I have an error during compilation when i do that.

That is my UserData 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)
}

But i have received an error:

  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)

I figured that it might be someting with tsconfig.json so here it is:

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

I am using npm run build this is react-scripts build in my project (4.0.2)

npm version 6.14.6
node version v12.18.3

Have i done anything wrong there? or is it something with my project settings?

83 thoughts on “Typescript Contructor Parameters. Parsing error: Unexpected reserved word 'public'”

  1. I have been exploring for a little bit for any high quality articles or weblog posts on this sort of house .
    Exploring in Yahoo I at last stumbled upon this website.
    Studying this info So i’m glad to express that I’ve an incredibly good uncanny feeling I found out exactly what I needed.
    I so much indisputably will make sure to don?t overlook this web site and provides it
    a glance regularly. http://www.deinformedvoters.org/dapoxetine-60mg

    Reply

Leave a Comment