VSCode中types.ts文件出现interface相关ESLint解析错误如何解决?
解决ESLint提示"Unexpected reserved word 'interface'"错误
这个错误是因为ESLint默认用JavaScript解析器处理你的.ts文件,不认识TypeScript特有的语法(比如interface)。按以下步骤修复:
安装TypeScript ESLint依赖
在项目根目录运行:npm install --save-dev @typescript-eslint/parser @typescript-eslint/eslint-plugin配置ESLint支持TypeScript
找到项目里的ESLint配置文件(比如.eslintrc.js、.eslintrc.json),修改成如下内容:module.exports = { parser: '@typescript-eslint/parser', // 指定使用TypeScript解析器 extends: [ 'plugin:@typescript-eslint/recommended' // 启用TypeScript专属ESLint规则 ], plugins: ['@typescript-eslint'], env: { node: true, es6: true } };如果项目里还没有
tsconfig.json,建议创建一个基础配置文件,让TypeScript解析器能正确识别项目结构:{ "compilerOptions": { "target": "ES6", "module": "CommonJS", "strict": true }, "include": ["src/**/*"] }重启VSCode生效
按Ctrl+Shift+P打开命令面板,选择"Reload Window",让ESLint重新加载配置。
完成以上步骤后,ESLint就能正确识别TypeScript语法,这个错误就会消失了。
内容的提问来源于stack exchange,提问作者Ballazx
相关产品推荐
相关产品推荐

