final-form / final-form

🏁 Framework agnostic, high performance, subscription-based form state management

Home Page:https://final-form.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Partial initialization initialValues

dartess opened this issue · comments

Are you submitting a bug report or a feature request?

bug report

What is the current behavior?

  1. use TS
  2. use interface for fields:
interface Fields {
	firstName: string;
	lastName: string;
}
<Form<Fields> />
  1. use initialValues as part of Fields
const initialValues = {
	firstName: 'John',
}
<Form<Fields> initialValues={initialValues} />
  1. get wrong TS error
Property 'lastName' is missing in type '{ firstName: string; }' but required in type 'Fields'.ts(2741)
index.tsx(11, 2): 'lastName' is declared here.
index.d.ts(267, 3): The expected type comes from property 'initialValues' which is declared here on type 'IntrinsicAttributes & FormProps<Fields>'

What is the expected behavior?

Now it is possible to either completely set initialValues, or not set at all. If I don’t know the exact set of all fields, but some of the fields should be set by default, I can’t do this in TS.

A probable solution to the problem is to replace the initialValues type from FormValues to Partial<FormValues>. But I do not know all the consequences of such a replacement.

Sandbox Link

https://codesandbox.io/s/react-typescript-playground-6k69p

What's your environment?

"final-form": "^4.18.6"

I made the same observation. Final Form clearly accepts partial initial values, so I'm not sure why it's not typed as Partial<FormValues>. Maybe just making a pull request may move things along.

My current workaround is doing this in react-final-form:

<Form initialValues={initialValues as FormData}/>

Published fix in v4.20.0.