Skip to content

fix: type for UseRemixFormReturn (#142)#181

Open
bravo-kernel wants to merge 1 commit into
code-forge-io:mainfrom
bravo-kernel:issue-142
Open

fix: type for UseRemixFormReturn (#142)#181
bravo-kernel wants to merge 1 commit into
code-forge-io:mainfrom
bravo-kernel:issue-142

Conversation

@bravo-kernel
Copy link
Copy Markdown

Fixes type issue described in #142.

Description

UseRemixFormReturn<TFieldValues> was defined using ReturnType<typeof useRemixForm> (without passing generic parameters) to capture the overridden signatures of handleSubmit, reset, and register. Because useRemixForm is a generic function, calling ReturnType<typeof useRemixForm> without type arguments resolves it with the default TFieldValues = FieldValues, causing reset and register to be typed against the base FieldValues instead of the caller's concrete schema type.

This makes UseRemixFormReturn<ConcreteSchema> unsatisfiable in practice: the value returned by useRemixForm<ConcreteSchema>() cannot be assigned to a parameter typed as UseRemixFormReturn<ConcreteSchema> because the reset and register signatures disagree on whether the field type is FieldValues or ConcreteSchema.

The fix replaces the manual intersection with a TypeScript instantiation expression (available since TS 4.7):

// Before
UseFormReturn<TFieldValues, TContext, TTransformedValues> & {
  handleSubmit: ReturnType<typeof useRemixForm>["handleSubmit"];
  reset:        ReturnType<typeof useRemixForm>["reset"];
  register:     ReturnType<typeof useRemixForm>["register"];
}

// After
ReturnType<typeof useRemixForm<TFieldValues, TContext, TTransformedValues>>

This derives the type directly from useRemixForm's actual return, with all three generic parameters propagated correctly, while preserving the same intent: the overridden handleSubmit, reset, and register signatures are still reflected (not the base UseFormReturn ones).

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Verified by type-checking a real application against the patched build. The app calls useRemixForm<FormData>() (where FormData is a concrete Zod schema type) and passes the result to a shared helper typed as UseRemixFormReturn<FieldValues>. Before the fix this required a @ts-expect-error suppression; after the fix the helper can be made generic (UseRemixFormReturn<T>) and all errors are gone with no suppressions needed.

Steps to reproduce the original error:

import { useRemixForm, type UseRemixFormReturn } from "remix-hook-form";
import type { FieldValues } from "react-hook-form";

// Simulates a shared helper that accepts any remix form
function helper(form: UseRemixFormReturn<FieldValues>) {}

const form = useRemixForm<{ email: string }>({ ... });

helper(form); // TS2345 before fix, compiles cleanly after
  • Unit tests

Checklist:

  • My code follows the guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

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

Successfully merging this pull request may close these issues.

1 participant