Skip to content

[react-hooks/exhaustive-deps] Generic Type parameters are being included in dependency array warning #2522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
charliematters opened this issue Sep 8, 2020 · 1 comment
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@charliematters
Copy link

charliematters commented Sep 8, 2020

  • [ x ] I have tried restarting my IDE and the issue persists.
  • [ x ] I have updated to the latest version of the packages.
  • [ x ] I have read the FAQ and my problem is not listed.

Repro

When using generic react components a warning about the generic type is fired about dependency arrays. It only fires when the parser is a version 4 or higher - this did not occur with version 3.8

React Hook React.useCallback has a missing dependency: 'T'. Either include it or remove the dependency array

{
  "rules": {
    "@typescript-eslint/explicit-module-boundary-types": "off",
    "react-hooks/exhaustive-deps": "warn",
  }
}
// Libraries
import * as React from 'react'

type EntityWithId = { id: string }

export interface Props<T extends EntityWithId> {
  entities: T[]
  toggleSelected: (entity: T) => void
}

function EntityTable<T extends EntityWithId>({
  entities,
  toggleSelected,
}: Props<T>) {
  const toggleSelection = React.useCallback(
    (entity: T) => {
      toggleSelected(entity)
    },
    [toggleSelected]
  )

  return (
    <>
      {entities.map((entity) => (
        <button key={entity.id} onClick={() => toggleSelection(entity)}>
          Toggle Me
        </button>
      ))}
    </>
  )
}

export default EntityTable
"compilerOptions": {
        "allowJs": true,
        "jsx": "react",
        "target": "es2019",
        "module": "esnext",
        "moduleResolution": "Node",
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "importsNotUsedAsValues": "error",
        "noImplicitAny": false,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "strictBindCallApply": true,
        "strictFunctionTypes": true,
        "strictNullChecks": false,
        "strictPropertyInitialization": false,
}

Expected Result

No warning
Actual Result

19:5 warning React Hook React.useCallback has a missing dependency: 'T'. Either include it or remove the dependency array react-hooks/exhaustive-deps

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 4.1.0
@typescript-eslint/parser 4.1.0
TypeScript 4.0.2
ESLint 7.8.1
node 14.4.0
@charliematters charliematters added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Sep 8, 2020
@bradzacher
Copy link
Member

As mentioned in other issues (#2513, #2479, #2476) this is working as intended.

The lint rule makes assumptions that no longer hold true, they need to fix it.

Please use the search next time.

@bradzacher bradzacher added duplicate This issue or pull request already exists and removed triage Waiting for team members to take a look labels Sep 8, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants