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

Review useForm types #9

Merged
merged 1 commit into from
Aug 10, 2024
Merged

Conversation

pedroborges
Copy link

After installing @types/lodash I got different type errors:

/inertia/packages/svelte/src/lib/useForm.ts:96:34
Error: No overload matches this call.
  Overload 1 of 4, '(target: {}, source: TForm): {} & TForm', gave the following error.
    Argument of type 'TForm | undefined' is not assignable to parameter of type '{}'.
      Type 'undefined' is not assignable to type '{}'.
  Overload 2 of 4, '(target: object, ...sources: any[]): any', gave the following error.
    Argument of type 'TForm | undefined' is not assignable to parameter of type 'object'.
      Type 'undefined' is not assignable to type 'object'. 
      if (typeof fieldOrFields === 'undefined') {
        defaults = Object.assign(defaults, cloneDeep(this.data()))



/inertia/packages/svelte/src/lib/useForm.ts:101:7
Error: Type '{ [x: string]: string | undefined; } | ({} & keyof TForm)' is not assignable to type 'TForm | undefined'.
  Type '{ [x: string]: string | undefined; }' is not assignable to type 'TForm'.
    '{ [x: string]: string | undefined; }' is assignable to the constraint of type 'TForm', but 'TForm' could be instantiated with a different subtype of constraint 'FormDataType'. 

      defaults = Object.assign(
        cloneDeep(defaults),


/inertia/packages/svelte/src/lib/useForm.ts:102:9
Error: No overload matches this call.
  Overload 1 of 4, '(target: {}, source: keyof TForm | { [x: string]: string | undefined; }): { [x: string]: string | undefined; } | ({} & keyof TForm)', gave the following error.
    Argument of type 'TForm | undefined' is not assignable to parameter of type '{}'.
      Type 'undefined' is not assignable to type '{}'.
  Overload 2 of 4, '(target: object, ...sources: any[]): any', gave the following error.
    Argument of type 'TForm | undefined' is not assignable to parameter of type 'object'.
      Type 'undefined' is not assignable to type 'object'. 
      defaults = Object.assign(
        cloneDeep(defaults),
        cloneDeep(typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields),


/inertia/packages/svelte/src/lib/useForm.ts:109:13
Error: Type 'TForm | undefined' is not assignable to type 'TForm'.
  'TForm' could be instantiated with an arbitrary type which could be unrelated to 'TForm | undefined'. 
    reset(...fields) {
      const resolvedData: TForm = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data!())
      const clonedData = cloneDeep(resolvedData) as TForm

This PR fixes those errors by added a check to convert the input data to an object if it's a function early in the useForm function.

I have also added other smaller type suggestions.

@jamesst20 jamesst20 merged commit 0c3809f into jamesst20:ts-fixes Aug 10, 2024
1 of 5 checks passed
@pedroborges pedroborges deleted the pedro-ts-fixes branch August 12, 2024 15:54
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.

2 participants