Skip to content

QuizNot attempted

forwardRef swallows generics

In onPick, v comes out as unknown. What is the React 19 fix?
const Input = forwardRef(<T,>(props: { value?: T; onPick?: (v: T) => void }, ref: Ref<HTMLInputElement>) => …);
<Input value={1} onPick={(v) => v.toFixed()} />
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • Drop forwardRef and take ref as a normal prop: function Input<T>({ ref, ...props }: Props<T> & { ref?: Ref<HTMLInputElement> })
  • forwardRef returns a non-generic ForwardRefExoticComponent<P>, so T is fixed once (to unknown) when it is called
  • in React 19 function components receive ref as an ordinary prop, so a plain generic function keeps inference per use.
  • Not forwardRef<number>: that pins a single type for every use and its generics are <Element, Props> anyway.
  • Not as any: removes all checking.
  • Not T extends {}: the constraint doesn't stop the generic being erased.