A bare FormRef types the form data as unknown
Detect
- In your code:
createRef<FormRef>(),useRef<FormRef>(null)or aRefObject<FormRef>with no type argument. - The type checker reports
'formData' is of type 'unknown'.(TS18046) in theonChangeoronSubmithandler of a form with a typedformData, or on the valuegetFormData()returns, becauseForminferredunknownfrom the ref.
Change
A bare FormRef defaults to unknown, and it can make Form infer unknown for T even when formData is typed. Supply the form-data type explicitly:
-const formRef = useRef<FormRef>(null);
+const formRef = useRef<FormRef<MyData>>(null);
The same goes for createRef<FormRef<MyData>>().
If your ref is still typed as the Form class, or reads other members through it, see A ref to Form holds a FormRef handle, not the class instance.
Verify
- The form's handlers and
formRef.current?.getFormData()are typed withMyData, and the type checker reports nounknownerror on them. - Nothing changes at runtime.
Also read: