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

How to type the methods of useRemixForm() when passing to a child component? #142

Open
AndresDJonge opened this issue Dec 19, 2024 Discussed in #133 · 0 comments
Open

Comments

@AndresDJonge
Copy link

Discussed in #133

Originally posted by AndresDJonge December 6, 2024
Hi all! I've got an issue with TypeScript when I'm trying to pass the form to a child component. I remember I could do this without a problem with the regular react-hook-form but now I've got this problems. Let's take an example:

enum Action {
    POST = 'post',
}

const loginFormSchema = z
    .object({
        action: z.literal(Action.POST),
        email: z.string().email('Email is required'),
        password: z.string().min(1, 'Password is required'),
    })
    .strict();

const loginSchema = z.discriminatedUnion('action', [loginFormSchema]);

function Home() {
  const loginForm = useRemixForm<z.infer<typeof loginSchema>>({
        resolver: zodResolver(loginSchema),
        defaultValues: {
            action: Action.POST,
        },
        submitConfig: {
            method: 'POST',
        },
    });
    
    return (
      <Child form={loginForm}> // TS Error
    )
}

interface ChildProps {
  form: UseRemixFormReturn;
}

function Child({form}: ChildProps) {
  ...
}

The TS Error I get is: Types of property register are incompatible. Type '(name: "action" | "email" | "password", options?: (RegisterOptions<{ action: Action; email: string; password: string; }> & { disableProgressiveEnhancement?: boolean | undefined; }) | undefined) => { ...; }' is not assignable to type '(name: string, options?: (RegisterOptions<FieldValues> & { disableProgressiveEnhancement?: boolean | undefined; }) | undefined) => { defaultValue?: string | undefined; ... 11 more ...; disabled?: boolean | undefined; }'. Types of parameters name and name are incompatible. Type string is not assignable to type 'action' | 'email' | 'password'

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