airjp73 / rvf

Easy form validation and state management for React and Remix
https://rvf-js.io
MIT License
840 stars 66 forks source link

[Feature]: Export ValidatedFormProps type #309

Closed lukejagodzinski closed 1 year ago

lukejagodzinski commented 1 year ago

What is the new or updated feature that you are suggesting?

When trying to create a wrapper component around the ValidatedForm component I've noticed a problem with lack of the ValidatedFormProps type. I want my wrapper component (UserForm) to have exactly the same interface as the ValidatedForm component plus a few extra props. But it doesn't properly infer types of generics.

type UserFormProps<T> = Omit<React.ComponentProps<typeof ValidatedForm>, "validator"> & { extraProp: string };

In this case I can't pass my generic T down to the ValidatedForm component. However, if this library exported the ValidatedFormProps type then it would work well.

type UserFormProps<T> = Omit<ValidatedFormProps<T>, "validator"> & { extraProp: string };

Unfortunatelly, it's limitation of TS and React.ComponentProps. I've done some research and it's not possible in TS without exporting the component props type.

Basically, it goes down to this problem:

function test<T>(arg: T): void {}

type Arg = Parameters<typeof test>[0];

type New<T extends string> = Arg<T>; // Error -> Type 'Arg' is not generic.ts(2315)

The generic information in the inferred type is lost.

Why should this feature be included?

It's just one extra import and it would improve DX when wrapping form component.

airjp73 commented 1 year ago

That sounds reasonable to me. Not exporting this sounds like an oversight. Would mind creating a PR?

lukejagodzinski commented 1 year ago

That sounds reasonable to me. Not exporting this sounds like an oversight. Would mind creating a PR?

OK thanks. I will create PR

lukejagodzinski commented 1 year ago

Actually I've just realized that there is form prop being exported but its name is just FormProps. Closing this issue. Sorry for bothering.