[英]TypeScript error inside JavaScript file in VSCode
我是 JavaScript 的新手。 我正在 React 应用程序中编写 JavaScript 文件,并且我在 class 中以这种方式声明私有 function notifyObservers
:
class Subject {
constructor(idOfDevice) {
//...
}
var notifyObservers = function (data) {
this.observers.forEach(obs => obs.notify(data));
}
getFrequency(idOfDevice) {
//...
}
}
正如我所见,建议这样做。 我在var
表示法上收到此错误:
Unexpected token. A constructor, method, accessor, or property was expected.ts(1068)
我在getFrequency
方法的{
上遇到错误:
';' expected.ts(1005)
我不明白为什么,因为在任何地方都看不到 TypeScript。 我敢肯定这只是一个愚蠢的新手错误,但我似乎无法弄清楚。
这就是我在package.json
文件中的内容:
{
"name": "name",
"version": "0.1.0",
"private": true,
"homepage": "http://localhost:3000",
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"avsc": "^5.4.18",
"axios": "^0.19.2",
"chart.js": "^2.9.3",
"dotenv": "^8.2.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-particles-js": "^2.7.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@iconify/icons-ant-design": "^1.0.6",
"@iconify/icons-bx": "^1.0.0",
"@iconify/icons-fe": "^1.0.3",
"@iconify/icons-ic": "^1.0.8",
"@iconify/icons-jam": "^1.0.3",
"@iconify/react": "^1.1.3"
}
}
我的node_modules
中仍然有一个@typescript-eslint
文件夹,并且在我的package-lock.json
中有这个:
"@typescript-eslint/eslint-plugin": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.27.0.tgz",
"integrity": "sha512-/my+vVHRN7zYgcp0n4z5A6HAK7bvKGBiswaM5zIlOQczsxj/aiD7RcgD+dvVFuwFaGh5+kM7XA6Q6PN0bvb1tw==",
"requires": {
"@typescript-eslint/experimental-utils": "2.27.0",
"functional-red-black-tree": "^1.0.1",
"regexpp": "^3.0.0",
"tsutils": "^3.17.1"
}
},
"@typescript-eslint/experimental-utils": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/experimental-utils/-/experimental-utils-2.27.0.tgz",
"integrity": "sha512-vOsYzjwJlY6E0NJRXPTeCGqjv5OHgRU1kzxHKWJVPjDYGbPgLudBXjIlc+OD1hDBZ4l1DLbOc5VjofKahsu9Jw==",
"requires": {
"@types/json-schema": "^7.0.3",
"@typescript-eslint/typescript-estree": "2.27.0",
"eslint-scope": "^5.0.0",
"eslint-utils": "^2.0.0"
},
"dependencies": {
"eslint-utils": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-2.0.0.tgz",
"integrity": "sha512-0HCPuJv+7Wv1bACm8y5/ECVfYdfsAm9xmVb7saeFlxjPYALefjhbYoCkBjPdPzGH8wWyTpAez82Fh3VKYEZ8OA==",
"requires": {
"eslint-visitor-keys": "^1.1.0"
}
}
}
},
"@typescript-eslint/parser": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-2.27.0.tgz",
"integrity": "sha512-HFUXZY+EdwrJXZo31DW4IS1ujQW3krzlRjBrFRrJcMDh0zCu107/nRfhk/uBasO8m0NVDbBF5WZKcIUMRO7vPg==",
"requires": {
"@types/eslint-visitor-keys": "^1.0.0",
"@typescript-eslint/experimental-utils": "2.27.0",
"@typescript-eslint/typescript-estree": "2.27.0",
"eslint-visitor-keys": "^1.1.0"
}
},
"@typescript-eslint/typescript-estree": {
"version": "2.27.0",
"resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-2.27.0.tgz",
"integrity": "sha512-t2miCCJIb/FU8yArjAvxllxbTiyNqaXJag7UOpB5DVoM3+xnjeOngtqlJkLRnMtzaRcJhe3CIR9RmL40omubhg==",
"requires": {
"debug": "^4.1.1",
"eslint-visitor-keys": "^1.1.0",
"glob": "^7.1.6",
"is-glob": "^4.0.1",
"lodash": "^4.17.15",
"semver": "^6.3.0",
"tsutils": "^3.17.1"
}
}
即使我似乎无法删除这些:如果我这样做了,一旦我执行npm start
启动我的应用程序,它们就会重新出现。
I have uninstalled typescript via npm uninstall -g typescript
and also via npm uninstall --save typescript
此外,添加
"typescript.validate.enable": false,
对我的settings.json
文件没有帮助。
VS Code 使用 TypeScript 为其 JavaScrip IntelliSense 供电。 这里的错误文本告诉你你需要的一切, ts
位是无关紧要的。
错误信息是:
A constructor, method, accessor, or property was expected
这告诉您类只能包含这些元素。 但是,您的代码在 class 中声明了一个var
,而 var 不在预期元素列表中。 这意味着您的代码有语法错误并且无效 JavaScript。
有关如何在 JS 类中声明私有属性,请参阅这样的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.