Generation hooks' `onResult` transform fails type inference (param is `any`, hard error under strict) #848
Send to my agentSummary#
The onResult transform callback on every generation hook (useGeneration and the wrappers built on it — useTranscription, useSummarize, useGenerateImage, useGenerateAudio, useGenerateSpeech, useGenerateVideo) fails to type-infer. Passing an onResult callback makes its parameter implicitly any and, under strict, is a hard compile error. The hook's result type also never narrows to the transform's return.
This affects all five framework packages (ai-react, ai-solid, ai-vue, ai-svelte, ai-angular) — ~35 hook files share the same signature pattern.
Repro#
import { useTranscription } from '@tanstack/ai-react'
const { result } = useTranscription({
connection: fetchServerSentEvents('/api/transcribe'),
onResult: (res) => res.text, // <- res is implicitly `any`
})
tsc --strict reports:
error TS2322: Type '(res: any) => any' is not assignable to type 'undefined'.
error TS7006: Parameter 'res' implicitly has an 'any' type.
result also stays TranscriptionResult | null instead of narrowing to the transform's return type.
Root cause#
useGeneration (and each wrapper) declares the transform type parameter with a default and infers it from a nested optional property:
// packages/ai-react/src/use-generation.ts:91-99
export function useGeneration<
TInput extends Record<string, any>,
TResult,
TOnResult extends ((result: TResult) => any) | undefined = undefined,
>(
options: Omit<UseGenerationOptions<TInput, TResult>, 'onResult'> & {
onResult?: TOnResult
},
): UseGenerationReturn<InferGenerationOutput<TResult, TOnResult>>
TypeScript does not infer a defaulted type parameter from an optional property nested inside the argument object, so TOnResult collapses to its default undefined. That makes onResult?: TOnResult become onResult?: undefined, which (a) rejects any function and (b) leaves the callback parameter with no contextual type.
Scope#
Same pattern in all generation hooks across every framework (~35 files), e.g.:
packages/ai-react/src/{use-generation,use-transcription,use-summarize,use-generate-image,use-generate-audio,use-generate-speech,use-generate-video}.tspackages/ai-solid/src/use-*.ts,packages/ai-vue/src/use-*.tspackages/ai-svelte/src/create-*.svelte.tspackages/ai-angular/src/inject-*.ts
It only went unnoticed because no example/test passes onResult to these hooks.
Suggested fix#
Replace the defaulted-generic signature with a two-overload pattern (one overload constrained by TOnResult extends (result: TResult) => any for the with-transform case, one without), which infers correctly. This is exactly the fix applied to the new useAudioRecorder's onComplete (modeled on, and now diverging from, this broken onResult pattern) — its overload approach can serve as the template. Adding expectTypeOf assertions per hook would prevent regressions, since the current type tests don't cover the onResult path.
Workaround#
Pass the type argument explicitly:
useTranscription<(r: TranscriptionResult) => void>({ onResult: (r) => { /* ... */ } })
Environment#
- TanStack AI:
main - TypeScript: 5.9.3 (
strict: true)
tombeckenham added labelsbug
AlemTuzlak closed this
Sign in to join this conversation.