[英]TypeScript error inside JavaScript file in VSCode
I am new to JavaScript.我是 JavaScript 的新手。 I am writing a JavaScript file in a React app, and I'm declaring a private function notifyObservers
this way, inside a class:我正在 React 应用程序中编写 JavaScript 文件,并且我在 class 中以这种方式声明私有 function notifyObservers
:
class Subject {
constructor(idOfDevice) {
//...
}
var notifyObservers = function (data) {
this.observers.forEach(obs => obs.notify(data));
}
getFrequency(idOfDevice) {
//...
}
}
As I've seen advised to do.正如我所见,建议这样做。 I am getting this error here on the var
notation:我在var
表示法上收到此错误:
Unexpected token. A constructor, method, accessor, or property was expected.ts(1068)
And I'm getting an error on the {
of the getFrequency
method:我在getFrequency
方法的{
上遇到错误:
';' expected.ts(1005)
I don't understand why, as there is no TypeScript anywhere in sight.我不明白为什么,因为在任何地方都看不到 TypeScript。 And I'm sure it's just a dumb rookie mistake, but I can't seem to figure it out.我敢肯定这只是一个愚蠢的新手错误,但我似乎无法弄清楚。
This is what I have in the package.json
file:这就是我在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"
}
}
There still is a @typescript-eslint
folder in my node_modules
, and in my package-lock.json
there is this:我的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"
}
}
And even if I can't seem to be able to delete those: if I do, they reappear as soon as i do npm start
to start my application.即使我似乎无法删除这些:如果我这样做了,一旦我执行npm start
启动我的应用程序,它们就会重新出现。
I have uninstalled typescript via npm uninstall -g typescript
and also via npm uninstall --save typescript
I have uninstalled typescript via npm uninstall -g typescript
and also via npm uninstall --save typescript
Also, adding此外,添加
"typescript.validate.enable": false,
to my settings.json
file does not help.对我的settings.json
文件没有帮助。
VS Code uses TypeScript to power its JavaScrip IntelliSense. VS Code 使用 TypeScript 为其 JavaScrip IntelliSense 供电。 The error text here tells you everything you need, the ts
bit is irrelevant.这里的错误文本告诉你你需要的一切, ts
位是无关紧要的。
The error message is:错误信息是:
A constructor, method, accessor, or property was expected
This is telling you that classes can only contain those elements.这告诉您类只能包含这些元素。 However your code is declaring a var
inside the class, and vars are not in the list of expected elements.但是,您的代码在 class 中声明了一个var
,而 var 不在预期元素列表中。 This means you code has a syntax error and is not valid JavaScript.这意味着您的代码有语法错误并且无效 JavaScript。
See answers like this for how to declare private properties in JS classes有关如何在 JS 类中声明私有属性,请参阅这样的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.