3

I have the following function:

export const getDocumentFile = async (
  docInstance: DocumentInstance,
  callback: ({message}: Error) => void, //   <---- ERROR HERE
): Promise<string | null> => {
  let document = null
  await axiosInstance
    .get('example.com')
    .then(response => {
      document = response.data;
    })
    .catch(error => {
      callback(error);
    });
  return document;
};

It accepts a callback which has the exact signature specified above, so I've typed it this way here, however I'm getting an error:

'message' is defined but never used. eslint(no-unused-vars)

This is the exact signature that the callback that I pass to this function has, so I'm unsure why it's throwing an error at all.

Changing the function signature so something else like callback: (error: Error) => void resultsin the exact same error.

I just just recently updated my react-scripts to 4.0.0 and I believe this has something to do with the error.

eslintrc.js

module.exports = {
  parser: '@typescript-eslint/parser',
  env: {
    es6: true,
    node: true,
    browser: true,
  },
  parserOptions: {
    ecmaVersion: 6,
    tsconfigRootDir: __dirname,
    project: './tsconfig.json',
    ecmaFeatures: {
      jsx: true,
    },
  },
  plugins: ['react', 'react-hooks', '@typescript-eslint'],
  rules: {
    'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
    'react-hooks/exhaustive-deps': 'warn', // Checks effect dependencies
    'no-use-before-define': 'off',
    '@typescript-eslint/no-use-before-define': ['error'],
  },
  extends: [
    'eslint:recommended',
    'plugin:react/recommended',
    // 'plugin:prettier/recommended',
  ],
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["es6", "dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noFallthroughCasesInSwitch": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "build-system",
    "src",
    ".eslintrc.js",
    "pretty.js",
    "gulpfile.js"
  ],
  "exclude": ["node_modules"]
}
Tyler
  • 1,163
  • 16
  • 37
  • Well it seems pretty obvious that the parameter `message` is in fact never used. – Pointy Nov 10 '20 at 17:54
  • 3
    @Pointy it's a type annotation, not a function definition. – VLAZ Nov 10 '20 at 17:55
  • @VLAZ oh I see, yea you're right. – Pointy Nov 10 '20 at 17:57
  • 3
    The issue is with the core ESLint `no-unused-vars` rule. It doesn't play nice with TypeScript. You can use the `@typescript-eslint/no-unused-vars` rule which comes from a separate package and is configured to work with TS code. It should be identical otherwise. – VLAZ Nov 10 '20 at 18:03
  • @VLAZ Thank you, that solved it. This was just one of many errors that showed up. Should I stop using `eslint:recommended` altogether, or just use `plugin:@typescript-eslint/recommended'` and `'plugin:@typescript-eslint/eslint-recommended'` as well? – Tyler Nov 10 '20 at 18:15
  • 1
    Oh, `eslint:recommended` is fine. To my knowledge, it's just that one rule that has a problem. You can still keep it - it has sensible defaults. I suggest also using the @typescript-eslint stuff. Here is what I have in my own personal ESLint config file: `"extends": [ "eslint:recommended", "plugin:@typescript-eslint/eslint-recommended", "plugin:@typescript-eslint/recommended" ]` and then go with more specific rules to tailor the config to my liking. But the recommended settings are definitely a sensible starting point. You can disable what you don't like, of course. – VLAZ Nov 10 '20 at 18:19

0 Answers0