query icon indicating copy to clipboard operation
query copied to clipboard

[lint]: no-rest-destructuring does not report on custom hooks

Open zbarbuto opened this issue 8 months ago • 5 comments

Describe the bug

When using custom hooks for a query function, the no-rest-destructuring does not report.

Steps to reproduce

// Custom hook for query
const useTodos = () => {
  return useQuery({
    queryKey: ['todos'],
    queryFn: () => fetch('example.com/todos'),
  });
  // NB results the same with const result = useQuery(/* */); return result;
};

function TodoList() {
  // ✅ Normal usage - issue reported
  const { dataA, ...restA } = useQuery({
    queryKey: ['todos'],
    queryFn: () => fetch('example.com/todos'),
  });

  // ❌ Custom hook - no issue reported
  const { dataB, ...restB } = useTodos();

  return <></>;
}

Expected behavior

Should report destructuring error when using const { ...rest } = useCustomQueryHook()

TanStack Query version

React query v5.50.1, eslint-plugin-query v5.71.5

TypeScript version

5.8.2

zbarbuto avatar Apr 04 '25 00:04 zbarbuto

yeah I’ve seen that too, not sure if it’s doable, especially if the hook is imported from a different file. @Newbie012 what do you think?

TkDodo avatar Apr 04 '25 07:04 TkDodo

It is if I check the hook's return type, but it will require type checking and will work only for projects using typescript-eslint.

Newbie012 avatar Apr 04 '25 12:04 Newbie012

if we want this, I think we’d need a separate config recomended-typechecking ?

TkDodo avatar Apr 04 '25 12:04 TkDodo

Importing from a different file is pretty much my main use-case. TypeScript-only would be fine IMO, even if it was separate config.

zbarbuto avatar Apr 06 '25 21:04 zbarbuto

if we want this, I think we’d need a separate config recomended-typechecking ?

Somewhat related to https://github.com/TanStack/query/pull/8966. I can add a flag for the rule to explicitly fallback to using ts.TypeChecker when using .recommendedTypeChecked

Newbie012 avatar Apr 07 '25 10:04 Newbie012