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

Adding a class with addSerializableClass() shows an error #32

Closed
wojexe opened this issue Sep 22, 2022 · 4 comments · Fixed by #35
Closed

Adding a class with addSerializableClass() shows an error #32

wojexe opened this issue Sep 22, 2022 · 4 comments · Fixed by #35
Labels
bug Something isn't working question Further information is requested version 2.x

Comments

@wojexe
Copy link

wojexe commented Sep 22, 2022

I have encountered the attached error message when I tried adding my class to be serializable with addSerializableClass().

This example 03-Store-Classes.md seems to show the error message as well.

Argument of type 'typeof NameHolder' is not assignable to parameter of type'FunctionConstructor'.
  Types of property 'prototype' are incompatible.
    Type 'NameHolder' is missing the following properties from type 'Function': apply, call, bind, prototype, and 4 more. js(2345)

edit:
I understand, this might not be a problem of package itself, but I think that it's worth mentioning, that such error might occur. As for now, I was able to just ignore the error and everything seems to work as expected.
I have also created a quick CodeSandbox with the example and there is no such error in there.

@MacFJA MacFJA added bug Something isn't working question Further information is requested version 2.x labels Oct 2, 2022
@MacFJA
Copy link
Owner

MacFJA commented Oct 2, 2022

It's indeed not a true error as the code won't stop working.
But the typing is not correct, I have to find the correct one.

Maybe something like 🤔

type ClassDefinition<T> = new (...args:any[]) => T;
export const addSerializableClass = (classDef: FunctionConstructor|ClassDefinition<any>): void => {
  addSerializable(classDef)
}

@wojexe
Copy link
Author

wojexe commented Oct 7, 2022

This type works for me.

The only thing is - it doesn't work with private class constructors - and it was probably not intended to work with those. It might be worth mentioning that in the docs, if it's not already there :)

FYI, the error that's showing up when the constructor is private, is nearly identical to the one shown before. This line is preceding the rest: Argument of type 'typeof NameHolder' is not assignable to parameter of type 'FunctionConstructor | ClassDefinition<any>'.

@MacFJA
Copy link
Owner

MacFJA commented Oct 8, 2022

The only thing is - it doesn't work with private class constructors - and it was probably not intended to work with those. It might be worth mentioning that in the docs, if it's not already there :)

It's possible, but it needs a bit more configuration:

  • First install @macfja/serializer as a project dependency
  • next in your code (in the earliest position, ie. the booting of the App):
import { addClassHandler, serialize, deserialize, addGlobalAllowedClass } from "@macfja/serializer"
import { setSerializer } from "@macfja/svelte-persistent-store"

addClassHandler(
  "MyClass",
  (source: MyClass, next) => {
    // Transform the class into a simple object.
    // `next` will serialize the given parameter
    return { prop1: source.prop1, property2: next(source.prop2) }
  },
  (source, next): MyClass => {
    // Create a new instance from the simple object.
    // `next` will deserialize the given parameter
    return MyClass.createInstance(source.prop1, next(source.property2))
  }
)
setSerializer(serialize, deserialize, addGlobalAllowedClass)
// And no need to do `addSerializableClass(MyClass)`

@MacFJA
Copy link
Owner

MacFJA commented Oct 11, 2022

@wojexe I just publish the version 2.2.1 that resolve the issue.
(The @macfja/serializer lib have also been updated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested version 2.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants