You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
// componentconst{ form$, validate }=useZodForm(signInSchema,{email: '',password: ''});// hookimport{useObservable,useObserve}from'@legendapp/state/react';import{z}from'zod';exportfunctionuseZodForm<Textendsz.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;/ }>*/constform$=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!
The text was updated successfully, but these errors were encountered:
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
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.
Pattern 1
with this pattern form is type
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
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!
The text was updated successfully, but these errors were encountered: