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

Type '(payload: Partial<Pokemon>) => Pokemon' is not assignable to type 'OnAddedFn' #76

Closed
vdsbenoit opened this issue Apr 3, 2022 · 6 comments Β· May be fixed by #77
Closed

Type '(payload: Partial<Pokemon>) => Pokemon' is not assignable to type 'OnAddedFn' #76

vdsbenoit opened this issue Apr 3, 2022 · 6 comments Β· May be fixed by #77
Assignees
Labels
bug Something isn't working

Comments

@vdsbenoit
Copy link

Hi @mesqueeb,

First of all, thanks a lot for the great work you have done here ! It is much appreciated πŸ™‡β€β™‚οΈ

I am trying to configure magnetar in a project I recently started with Vue 3. However, I am having trouble to make it work with TypeScript. To simplify my description, I am going to use the example provided in your documentation.

When I try to transpile this snippet :

export type Pokemon = { name: string, nickName?: string, id: string, level: number }

export function pokemonDefaults(payload: Partial<Pokemon>): Pokemon {
  const defaults: Pokemon = { name: '', nickName: '', id: '', level: 0 }

  return { ...defaults, ...payload }
}

// here is how you inject the Type: collection<Pokemon>
export const pokedexModule = magnetar.collection<Pokemon>('pokedex', {
  modifyPayloadOn: { insert: pokemonDefaults },
  modifyReadResponseOn: { added: pokemonDefaults },
})

I get this error:

ERROR in src/services/pokemon.ts:18:27
TS2322: Type '(payload: Partial<Pokemon>) => Pokemon' is not assignable to type 'OnAddedFn'.
  Types of parameters 'payload' and 'docData' are incompatible.
    Type 'Record<string, any> | undefined' is not assignable to type 'Partial<Pokemon>'.
      Type 'undefined' is not assignable to type 'Partial<Pokemon>'.
    16 | export const pokedexModule = magnetar.collection<Pokemon>('pokedex', {
    17 |   modifyPayloadOn: { insert: pokemonDefaults },
  > 18 |   modifyReadResponseOn: { added: pokemonDefaults },
       |                           ^^^^^
    19 | })
    20 |

Do you have an idea what could have gone wrong?

Here is my config

node 16.13.1
magnetar 0.5.4
typescript 4.6.2
@mesqueeb
Copy link
Member

mesqueeb commented Apr 4, 2022

@vdsbenoit thanks for finding this!

So I have typed the modifyReadResponseOn.added function kind of weirdly:

/**
 * Can be used to modify docs that come in from 'stream' or 'fetch' actions, before they are added to your store data. When returning `undefined` they will be discarded & won't be added to the store data.
 */
export type OnAddedFn = (docData: Record<string, any> | undefined, docMetadata: DocMetadata) => Record<string, any> | void

see source code at:

But I don't think we should ever execute the modifyReadResponseOn.added fn when the payload is undefined.

Changing my underlying type to this will fix the issue:

/**
 * Can be used to modify docs that come in from 'stream' or 'fetch' actions, before they are added to your store data. When returning `undefined` they will be discarded & won't be added to the store data.
 */
export type OnAddedFn = (docData: Record<string, any>, docMetadata: DocMetadata) => Record<string, any> | void

So I will do this somewhere this week after checking this won't cause issues.

In the meanwhile you can fix the issue on your end with a workaround of adding a simple ? in the defaults fn payload like so:

export function pokemonDefaults(payload?: Partial<Pokemon>): Pokemon {
  const defaults: Pokemon = { name: '', nickName: '', id: '', level: 0 }

  return { ...defaults, ...payload }
}

see the payload?: : ) that's the workaround that should work for now

@mesqueeb mesqueeb self-assigned this Apr 4, 2022
@mesqueeb mesqueeb added the bug Something isn't working label Apr 4, 2022
mesqueeb added a commit that referenced this issue Apr 4, 2022
@mesqueeb mesqueeb mentioned this issue Apr 4, 2022
2 tasks
@vdsbenoit
Copy link
Author

Thanks a lot for your quick reply ! I'll try that

@mesqueeb
Copy link
Member

@vdsbenoit you can now define the type of your document when inserting separately from the type of your module doc.

 const pokedexModule = magnetar.collection<Pokemon, { insert: Partial<Pokemon> }>('pokedex', 
  modifyPayloadOn: { insert: pokemonDefaults },
  modifyReadResponseOn: { added: pokemonDefaults },
})

this will make it so executing pokedexModule.insert({ name: 'bulbasaur' }) won't cause any issue if this is only a partial of your document.

Does this help you further?

@mesqueeb
Copy link
Member

@vdsbenoit on [email protected] I have changed the logic so modifyReadResponseOn.added is no longer executed when a fetch call returns no document.

This means that there is no possibility that the payload of modifyReadResponseOn.added is undefined anymore.

This means that the documentation example is now possible without type issues.

workaround: pokemonDefaults(payload?: Partial<Pokemon>): is no longer needed, you can use pokemonDefaults(payload: Partial<Pokemon>): without the ? again! πŸŽ‰

let me know if you have any further issues.

@vdsbenoit
Copy link
Author

Hi @mesqueeb,

Thanks a lot for the support! Unfortunately, I am no longer working on this personal project for the moment. But I will hopefully get back to it in a couple of months. While I am confident you have fixed my problem, I will get back to you if I still face any issues.

Cheers,

Benoit

@vdsbenoit
Copy link
Author

Hi,

I'm diving into my side project again (it's for an event that takes place once a year). I upgraded magnetar from 0.5.4 to 0.11.4. I confirm the issue is resolved! Thanks again for your support !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants