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

Better support for exactOptionalPropertyTypes: true #697

Open
jakubmazanec opened this issue Jul 1, 2024 · 0 comments
Open

Better support for exactOptionalPropertyTypes: true #697

jakubmazanec opened this issue Jul 1, 2024 · 0 comments

Comments

@jakubmazanec
Copy link

Describe the bug and the expected behavior

When TypeScript compiler option exactOptionalPropertyTypes is set to true, you cannot pass undefined as a value to an object property that is marked as optional. While IMO very useful option, it can lead to bad DX, because it can cause TypeScript errors:

import {useForm} from '@conform-to/react';

type CustomComponentProps = {
  onSubmit?: Parameters<typeof useForm>[0]['onSubmit'] | undefined;
}

let CustomComponent = ({onSubmit}: CustomComponentProps ) => {
  let [form] = useForm({
    onSubmit // error here, because `onSubmit` prop can be `undefined`
  });

 // rest of the component
}

I therefore prefer that "input" types (like function parameters, properties in options objects, etc.) have explicit undefined added to them; e.g. here I would like to have this:

export type FormOptions<
	Schema extends Record<string, any> = any,
	FormError = string[],
	FormValue = Schema,
> = BaseFormOptions<Schema, FormError, FormValue> & {
	/**
	 * A function to be called before the form is submitted.
	 */
	onSubmit?: ((
		event: FormEvent<HTMLFormElement>,
		context: ReturnType<
			BaseFormContext<Schema, FormError, FormValue>['submit']
		>,
	) => void) | undefined; // here I added explicit `undefined` to the type ⚠️
};

Would it be possible to add undefined to all input types? Thank you.

Conform version

v1.1.5

Steps to Reproduce the Bug or Issue

What browsers are you seeing the problem on?

No response

Screenshots or Videos

No response

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant