Skip to content

Bug: react-hooks/exhaustive-deps false positive when function is casted with TypeScript #20750

Closed
@0x24a537r9

Description

@0x24a537r9

React version: 17.0.1

Steps To Reproduce

Setup eslint with @typescript-eslint/parser as parser
Cast a function passed to useEffect

import {useCallback, useEffect} from 'react';

type F = (...args: unknown[]) => void;

function MyComp() {
	const foo = useCallback(() => {}, []);

	// OK
	useEffect(() => {
		foo();
	}, [foo]);

	// WARNS?
	useEffect((() => {
		foo();
	}) as F, [foo]);

	return 'Hello, world'
}

Link to code example: https://github.com/0x24a537r9/exhaustive-deps-bug

The current behavior

The following error was reported

  14:2  warning  React Hook useEffect received a function whose dependencies are unknown. Pass an inline function instead  

The expected behavior

The rule should interpret the function argument correctly and know that it is already inline.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions