0

Eslint unable to parse this typescript code.

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 }
}

enter image description here

I cannot disable Eslint on this line, I have to exclude this file in Eslint config.

The code itself however works as expected.

Eslint config

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
  • Have you tried closing and re-opening your IDE? Commented Oct 14, 2021 at 1:35
  • I did, the error persist Commented Oct 14, 2021 at 1:40
  • Can you share your eslint config please? Commented Oct 14, 2021 at 1:48

2 Answers 2

2

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.

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:

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

You can read more information about setting up eslint with typescript here

Sign up to request clarification or add additional context in comments.

3 Comments

I already have this parser, eslint can parse the rest of my typescript with no problem, only this line has issue
eslint config shared
Thanks. I don't see the parserOptions.project set, which you will definitely need. Also make sure that you have the latest version of eslint and all of the dependent plugins / config packages.
2

solved by update @typescript-eslint/parser and @typescript-eslint/eslint-plugin to 5.0.0

1 Comment

For me was to install "5.62.0" for both packages

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.