RTK Query – eslint gives errors @typescript-eslint/no-invalid-void-type if query's parameter is void

RTK Query – eslint gives errors @typescript-eslint/no-invalid-void-type if query's parameter is void

Problem Description:

I have a query function without parameter.

  endpoints: (build) => ({
    fetchEvents: build.query<Test[], void>({
      query: () => '/test'
    }),
  })

ES-Lint errors:

void is only valid as a return type or generic type argument
@typescript-eslint/no-invalid-void-type

What should be used instead of void?

Update

This is my .eslintrc.js

module.exports = {
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "plugin:react/recommended",
        "standard-with-typescript",
        'eslint:recommended',
        "prettier"
    ],
    "overrides": [

    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": "latest",
        "sourceType": "module",
        "project": "./tsconfig.json",
    },
    "plugins": [
        "react",
        "@typescript-eslint",
        "prettier"
    ],
    "rules": {
        "@typescript-eslint/consistent-type-definitions": ["error", "type"],
        
    },
    settings: {
        "react": {
            "version": "detect",
        },
    }
}

Solution – 1

This is a warning (or error) because someone in your project decided it should be an error – it has no technical reason and can be changed in your eslint configuration at any time.

Redux Toolkit uses the void type here – it is the correct way of doing this.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject