Skip to content

Bug: Dependencies Array shows missing dependency error for generic type parameters #19808

Closed
@mrousavy

Description

@mrousavy

React version: 16.13.1

Steps To Reproduce

  1. Create function with generic type parameter
  2. Use that generic type parameter in useCallback, useEffect or useMemo

Example:

export function useDocument<TDocument>(path: string, defaultValue: TDocument | undefined = undefined): TDocument | undefined {
	const [value, setValue] = useState<TDocument | undefined>(defaultValue);

	const onNext = useCallback((snapshot: FirebaseFirestoreTypes.DocumentSnapshot<FirebaseFirestoreTypes.DocumentData>) => {
		if (snapshot.exists) setValue(build<TDocument>(snapshot.data(), snapshot.id));
		else setValue(undefined);
	}, []);


	useEffect(() => firestore().doc(path).onSnapshot(onNext), [onNext, path]);

	return value;
}

Screenshot 2020-09-10 at 19 57 06

The current behavior

Shows error React Hook useCallback has a missing dependency: 'TDocument'. Either include it or remove the dependency array.eslintreact-hooks/exhaustive-deps)

The expected behavior

Should not show any errors, because it isn't possible to add types to the dependencies array (types don't exist at runtime!)

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