TypeScript条件类型CamelToSnakeCase遇ESLint解析错误求助
问题原因
你遇到的ESLint解析错误,是因为配置中引入的google ESLint风格指南是为JavaScript设计的,它的语法解析规则不兼容TypeScript 4.1+新增的模板字面量条件类型语法,导致ESLint无法正确识别这段类型定义代码,从而抛出Type expected错误。
解决方案
方案1:移除google扩展
直接在ESLint配置的extends数组中删除"google"项,消除与TypeScript高级类型的冲突:
module.exports = { root: true, env: { es6: true, node: true, }, extends: [ "eslint:recommended", "plugin:import/errors", "plugin:import/warnings", "plugin:import/typescript", "plugin:@typescript-eslint/recommended", ], parser: "@typescript-eslint/parser", parserOptions: { project: [ "tsconfig.json", "tsconfig.dev.json", ], sourceType: "module", }, ignorePatterns: [ "/lib/**/*", // Ignore built files. ], plugins: [ "@typescript-eslint", "import", ], rules: { "quotes": ["error", "double"], "import/no-unresolved": 0, }, };
方案2:使用TypeScript适配的Google风格规则
如果需要遵循Google代码风格,可改用@typescript-eslint/eslint-plugin提供的适配TypeScript的Google风格配置,替换原有的"google"扩展:
extends: [ "eslint:recommended", "plugin:import/errors", "plugin:import/warnings", "plugin:import/typescript", "@typescript-eslint/eslint-plugin/configs/google", "plugin:@typescript-eslint/recommended", ]
额外注意事项
- 确保
@typescript-eslint/parser和@typescript-eslint/eslint-plugin为最新版本,以支持TypeScript的最新语法特性。 - 修改配置后,通过VSCode命令面板执行
ESLint: Restart ESLint Server,让新配置生效。
内容的提问来源于stack exchange,提问作者Yone
相关产品推荐
相关产品推荐

