Replies: 2 comments
-
The type for props is rather broad so that you can define a lot of types. To preserve, say, an interface of your props, you can do the following: interface YourProps {
index: number;
variant: 'edit' | 'create';
}
// These values will never be used in the component, but
// You'll have to define them outside if you want to avoid 'lying'
const props: YourProps = {
index: 0,
variant: 'edit',
};
const YourComponent = withForm({
defaultValues,
props: { ...props }, // avoid assigning directly as it will expect a Record<string, unknown>
render: function Render(props) {
props.variant // 'edit' | 'create'
return <></>;
},
}); |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hello again! I just made a discovery that could make it easier for you to define the types. The extending features that interfaces have is the reason it is not assignable to Solution 1: type YourSubformProps = {
nullableFoo: string | null,
bar: SomeOtherType
}
const YourSubform = withForm({
props: {} as YourSubformProps // works!
}) Solution 2: // in a utils file
/*
* Convert any interface into a type which is compatible with `withForm.props`.
*/
export type FormPropsWrapper<T> = Omit<T, never>
interface YourSubformProps {
nullableFoo: string | null,
bar: SomeOtherType
}
const YourSubform = withForm({
props: {} as YourSubformProps, // errors, it's an interface
props: {} as FormPropsWrapper<YourSubformProps> // works!
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
It's advised that in-order to be able to composite forms the
withForm
HOC is required.From the docs
I'm all for type inference but when your component has props that are complicated you usually do something like this
This feels like a bit of an anti-pattern, I understand the goal to avoid generics but it feels a bit of an extreme here.
The reason it feels like an anti pattern is because as a developer you are "lying" (saying {} is a complicated type) in order to satisfy type safety.
My question is - is there a recommended approach to dealing with this situation without "lying"?
General thoughts are that It would feel better if it was more explicit perhaps
Where
withForm.props
has this implementationOr in general have this one exception to allow a generic type 😅.
Beta Was this translation helpful? Give feedback.
All reactions