diff --git a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js index 29e956d314a2..6be2c4afba1e 100644 --- a/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js +++ b/packages/eslint-plugin-react-hooks/__tests__/ESLintRuleExhaustiveDeps-test.js @@ -8009,6 +8009,21 @@ const testsTypescript = { } `, }, + { + code: normalizeIndent` + function useBug(val: T) { + const ref = useRef(val); + + const fn = () => { + const temp: T = ref.current; + }; + + useEffect(() => { + fn(); + }, []); + } + `, + }, { code: normalizeIndent` function MyComponent() { diff --git a/packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts b/packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts index 6b790680608d..1e56e8d6265e 100644 --- a/packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts +++ b/packages/eslint-plugin-react-hooks/src/rules/ExhaustiveDeps.ts @@ -446,6 +446,26 @@ const rule = { if (ref.resolved == null) { continue; } + const referenceNode = fastFindReferenceWithParent( + fnNode, + ref.identifier, + ); + if ( + referenceNode?.parent?.type === 'TSTypeQuery' || + referenceNode?.parent?.type === 'TSTypeReference' || + // @ts-expect-error Flow-specific AST node type + referenceNode?.parent?.type === 'GenericTypeAnnotation' + ) { + continue; + } + const def = ref.resolved.defs[0]; + if ( + def != null && + // @ts-expect-error We don't have flow types + def.type === 'TypeParameter' + ) { + continue; + } if ( pureScopes.has(ref.resolved.scope) && // Stable values are fine though,