Skip to main content
Version: Current (v7.0.0-beta.3)

Form data defaults to unknown and form context to FormContextType instead of any

Detect​

  • The type checker reports 'formData' is of type 'unknown'. (TS18046) where your code reads formData from an IChangeEvent, FieldProps, WidgetProps, a template's props or a customValidate callback written without a type argument.
  • In your code: IChangeEvent, FieldProps, WidgetProps, CustomValidator or a *TemplateProps type used bare, with code that reads into the data.
  • In your code: SchemaUtilsType<T, S> with two type arguments, written out by hand for a Registry's schemaUtils.

Change​

The RJSF generics are <T, S, F>: the form data, the schema and the form context. T and F used to default to any, so anything read out of formData was unchecked however far it travelled. They now default to unknown and FormContextType. FormContextType is object, so reading a key of an untyped formContext is a type error too; see FormContextType is object, so formContext keys must be named.

The form data now arrives as unknown, so name its type or narrow it. Naming the type is preferred:

-const onChange = ({ formData }: IChangeEvent) => save(formData.name);
+const onChange = ({ formData }: IChangeEvent<MyData>) => save(formData.name);

Narrow it when the data really is unknown at that point:

-function MyField({ formData }: FieldProps) {
- return <input value={formData} />;
+function MyField({ formData }: FieldProps) {
+ return <input value={typeof formData === 'string' ? formData : ''} />;
}

A custom validator receives unknown for the same reason:

-const customValidate = ({ pass1, pass2 }, errors) => {
+const customValidate: CustomValidator<MyData> = (formData, errors) => {
+ const { pass1, pass2 } = formData ?? {};
if (pass1 !== pass2) {
errors.pass2?.addError("Passwords don't match");
}
return errors;
};

Registry.schemaUtils is now SchemaUtilsType<T, S, F> rather than SchemaUtilsType<T, S>. If you wrote that type out by hand, add the third argument:

-const schemaUtils: SchemaUtilsType<MyData, RJSFSchema> = registry.schemaUtils;
+const schemaUtils: SchemaUtilsType<MyData, RJSFSchema, MyFormContext> = registry.schemaUtils;

Verify​

The type checker reports no is of type 'unknown' error on form data. Nothing changes at runtime. These keep working without a change:

Also read: