Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialValues of createFormStore should accept undefined if exactOptionalPropertyTypes is enabled #234

Open
Huliiiiii opened this issue Aug 3, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@Huliiiiii
Copy link
Contributor

if exactOptionalPropertyTypes is enabled, initialValues won't accept undefined but accpet {}

@fabian-hiller
Copy link
Owner

Can you send me a Modular Forms code example with more details?

@fabian-hiller fabian-hiller self-assigned this Aug 3, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Aug 3, 2024
@Huliiiiii
Copy link
Contributor Author

Huliiiiii commented Aug 3, 2024

https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes

export type FormOptions<TFieldValues extends FieldValues> = Partial<{
    initialValues: PartialValues<TFieldValues>;
    validateOn: ValidationMode;
    revalidateOn: ValidationMode;
    validate: ValidateForm<TFieldValues>;
}>;

@fabian-hiller
Copy link
Owner

Why is this a problem? Instead of undefined you can just not define any initialValues or am I missing something?

@Huliiiiii
Copy link
Contributor Author

Huliiiiii commented Aug 3, 2024

It can't accept a variable that might be undefined, although I can add ?? {} after it.

const schema = v.object({
	name: v.string()
})
const formStore = createFormStore<v.InferInput<typeof schema>>({
	validate: valiForm(schema),
	initialValues: (() => (Math.random() > 0.5 ? undefined : { name: "str" }))(),
})
/** 
Argument of type '{ validate: ValidateForm<{ name: string; }>; initialValues: { name: string; } | undefined; }' is not assignable to parameter of type 'Partial<{ initialValues: { name?: Maybe<string>; }; validateOn: ValidationMode; revalidateOn: ValidationMode; validate: ValidateForm<{ name: string; }>; }>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
  Types of property 'initialValues' are incompatible.
    Type '{ name: string; } | undefined' is not assignable to type '{ name?: Maybe<string>; }'.
      Type 'undefined' is not assignable to type '{ name?: Maybe<string>; }'.
*/

But it's just repeating what you've done in the code.

function createFormStore({
  initialValues = {},
  validateOn = 'submit',
  revalidateOn = 'input',
  validate
} = {})

@fabian-hiller
Copy link
Owner

I understand and agree that we should improve this for a better DX. In the meantime, I hope the workaround works for you.

@fabian-hiller fabian-hiller added enhancement New feature or request and removed question Further information is requested labels Aug 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants