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

Bug: Legend State v3 - Typescript Types Break when Passing Initial Values to useObserver #394

Open
mikecfisher opened this issue Nov 11, 2024 · 0 comments

Comments

@mikecfisher
Copy link

mikecfisher commented Nov 11, 2024

I am trying to create a reusable hook that uses legend-state and zod for form validation. I can get the code to work but it will not properly work with typescript when I pass initial values via a function. I've tried two different patterns and both don't create proper types.

"@legendapp/state": "^3.0.0-beta.17",

Pattern 1

// Component
  const { form$, validate } = useZodForm(signInSchema);
// hook
 const form$ = useObservable({
    values: {},
    fieldErrors: {},
    isLoading: false,
    formError: '',
    didSubmitAttempt: false,
  });

with this pattern form is type

const form$: Observable<{
   values: {};
   fieldErrors: {};
   isLoading: boolean;
   formError: string;
   didSubmitAttempt: boolean;
}>

With this approach I get types for everything but values as expected.
However I need to have types for the values that my form works with. example email/password.

Pattern 2

// component
  const { form$, validate } = useZodForm(signInSchema, {email: '', password: ''});

// hook
import { useObservable, useObserve } from '@legendapp/state/react';
import { z } from 'zod';

export function useZodForm<T extends z.ZodType>(schema: T, initialValues: z.infer<T>) {
/*typescript type for $form upon initialization equals:
/ const form$: Observable<{
/    values: z.TypeOf<T>;
/    fieldErrors: {};
/    isLoading: boolean;
/    formError: string;
/    didSubmitAttempt: boolean;
/ }>
*/

  const form$ = useObservable({
    values: initialValues,
    fieldErrors: {},
    isLoading: false,
    formError: '',
    didSubmitAttempt: false,
  });

// typescript error 
//Property 'isLoading' does not exist on type 'Observable<{ values: TypeOf<T>; fieldErrors: {}; isLoading: boolean; formError: string; didSubmitAttempt: boolean; }>'.ts(2339)

$form.isLoading.get()

Here I pass in types and infer the types for the initial values from the zodSchema passed in.
form$ initially has correct types when it's defined. But when I try to access it I get a typescript error.

I think this is a bug in the useObserver typescript definition but if I'm doing something incorrectly please let me know.

Thanks!

@mikecfisher mikecfisher changed the title Legend State v3 - Typescript Types Break when Passing Initial Values to useObserver Bug: Legend State v3 - Typescript Types Break when Passing Initial Values to useObserver Nov 11, 2024
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