打字稿构造函数参数。分析错误:意外的保留字"公共"



我想在typescript + react中创建一个类我决定使用Typescript构造函数参数但是当我这样做的时候,我在编译过程中有一个错误。

这是我的UserData类
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 version6.14.6节点版本v12.18.3

我做错什么了吗?还是我的项目设置有问题?

我设法通过将eslintConfig添加到我的package.json

来解决这个问题
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},

相关内容

  • 没有找到相关文章

最新更新