【问题标题】:typescript eslint parsing error "]" expected预期打字稿 eslint 解析错误“]”
【发布时间】:2021-10-14 10:30:27
【问题描述】:

Eslint 无法解析此打字稿代码。

export const swapKeyAndValue = <T extends { [index: string]: string }>(
    obj: T
) => {
    const newObj: { [prop: string]: string } = {}
    for (const prop in obj) {
        newObj[obj[prop]] = prop
    }
    return newObj as { [K in keyof T as T[K]]: K }
}

我无法在此行或文件上禁用 eslint,因为我必须在 eslint 配置中排除此文件。

代码本身按预期工作。

eslint 配置

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',
        'plugin:prettier/recommended',
    ],
    parser: '@typescript-eslint/parser',
    ignorePatterns: [
        'dist/**/*', // Ignore built files.
    ],
    plugins: ['@typescript-eslint', 'import'],
    rules: {
        'import/no-unresolved': 'off',
        '@typescript-eslint/explicit-module-boundary-types': 'off',
        '@typescript-eslint/no-explicit-any': 'error',
        camelcase: 'off',
    },
}

打字稿:3.9.7 eslint: 7.32.0

【问题讨论】:

  • 您是否尝试过关闭并重新打开您的 IDE?
  • 我做了,错误仍然存​​在
  • 你能分享一下你的 eslint 配置吗?

标签: typescript eslint


【解决方案1】:

如果没有 eslint 配置,很难确定,但听起来 eslint 不知道您正在使用 typescript,或者不知道您正在使用什么特定的 typescript 选项/版本。

我会确保你使用的是最新版本的 eslint + 你的 typescript-eslint 插件,并且在你的 eslint 配置中有这样的东西:

  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: './tsconfig.json',
  },

您可以阅读有关使用 typescript here 设置 eslint 的更多信息

【讨论】:

  • 我已经有了这个解析器,eslint 可以毫无问题地解析我的 typescript 的其余部分,只有这一行有问题
  • eslint 配置共享
  • 谢谢。我没有看到你肯定需要的parserOptions.project 集。还要确保你有最新版本的 eslint 和所有依赖的插件/配置包。
【解决方案2】:

已通过将 @typescript-eslint/parser@typescript-eslint/eslint-plugin 更新到 5.0.0 解决

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2023-03-25
    • 2018-03-06
    • 2013-08-20
    • 2021-06-05
    • 2017-09-27
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多