简体   繁体   English

预计打字稿 eslint 解析错误“]”

[英]typescript eslint parsing error "]" expected

Eslint unable to parse this typescript code. 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 }
}

在此处输入图片说明

I am not able to disable eslint on this line or file, as I have to exclude this file in eslint config.我无法在此行或文件上禁用 eslint,因为我必须在 eslint 配置中排除此文件。

The code itself however work as expected.然而,代码本身按预期工作。

eslint config 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',
    },
}

typescript: 3.9.7 eslint: 7.32.0打字稿:3.9.7 eslint:7.32.0

Without the eslint config, it's hard to say for sure, but it sounds like eslint doesn't know that you're using typescript, or doesn't know what specific typescript options / version you're using.没有 eslint 配置,很难确定,但听起来 eslint 不知道您正在使用打字稿,或者不知道您正在使用的具体打字稿选项/版本。

I would ensure that you are using the latest versions of eslint + your typescript-eslint plugins, and have something like this in your eslint config:我会确保你使用的是最新版本的 eslint + 你的 typescript-eslint 插件,并且在你的 eslint 配置中有这样的东西:

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

You can read more information about setting up eslint with typescript here您可以在此处阅读有关使用打字稿设置 eslint 的更多信息

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM