<!--
title: Prefer inferred, clear async
domain: app-frameworks
topic: Code Style
language: TSX
source: tanstack/query
updated: 2025-11-23
url: https://awesomereviewers.com/reviewers/query-prefer-inferred-clear-async/
-->

When writing code and tests, optimize for clarity and idiomatic TypeScript:

1) Prefer `async/await` when the async function includes a side effect.
```ts
// Clear: side effect is explicit
queryFn: async () => {
  await sleep(10)
  fetched = true
  return 'fetched'
}

// Less explicit when side effects are involved
queryFn: () => sleep(10).then(() => {
  fetched = true
  return 'fetched'
})
```

2) Let TypeScript infer callback types; avoid explicit parameter annotations/imports when they’re redundant.
```ts
// Prefer inference
onMutate: async (newTodo, context) => {
  // ...
}
// If the types infer correctly, remove extra imports/annotations.
```

3) Simplify type-level assertions by removing unnecessary generics/wrappers; use direct indexed access or the minimal expression that captures the intended type.
```ts
// Prefer minimal type plumbing
expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
```
