-
Notifications
You must be signed in to change notification settings - Fork 16
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
Comments
It's indeed not a true error as the code won't stop working. Maybe something like 🤔 type ClassDefinition<T> = new (...args:any[]) => T;
export const addSerializableClass = (classDef: FunctionConstructor|ClassDefinition<any>): void => {
addSerializable(classDef)
} |
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: |
It's possible, but it needs a bit more configuration:
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)` |
@wojexe I just publish the version |
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.
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.
The text was updated successfully, but these errors were encountered: