'Create type that would infer properties it needs to pick from another type from function type parameters
Here's a playground
I have a type
children: ({ register, formState }: Pick<UseFormReturn<T>, 'register' | 'formState') => ReactNode;
And I want to make it work with any parameters as long as they exist on type UseFormReturn. It needs to infer what parameters to pick based on the defined function parameters.
So for example if I wanted to add a parameter to function declaration type
children: ({ register, controller, formState }) .....
I want parameters type definition to change accordingly without the need of manually editing type to
...Pick<UseFormReturn<T>, 'register' | 'formState' | 'controller')...
I tried doing this
children: <K>({ register, formState }: Pick<UseFormReturn<T>, UseFormReturn<T>[K]>) => ...
// and this
children: <K extends keyof UseFormReturn<T>>({ register, formState }: Pick<UseFormReturn<T>, UseFormReturn<T>[K]>) => ...
// and this
children: <K>({ register, formState }: Pick<UseFormReturn<T>, Parameters[K]>) => ...
but couldn't make it work.
Please help me out guys
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
