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

Inaccurate Return Type for toJSON Method in RealmObject #6728

Open
CacaoRick opened this issue Jun 17, 2024 · 2 comments · May be fixed by #6789
Open

Inaccurate Return Type for toJSON Method in RealmObject #6728

CacaoRick opened this issue Jun 17, 2024 · 2 comments · May be fixed by #6789

Comments

@CacaoRick
Copy link

I've noticed a potential issue with the toJSON method's return type in the RealmObject class. According to the current TypeScript definition, the toJSON method is expected to return a DefaultObject. However, considering the generic type T used throughout the RealmObject<T> class, it seems more appropriate for toJSON to return a type T instead, especially since the method processes and returns a plain object representation of the instance.

Current Implementation:

export class RealmObject<T = DefaultObject, RequiredProperties extends keyof OmittedRealmTypes<T> = never> {

  // ...

  toJSON(_?: string, cache?: unknown): DefaultObject;
  toJSON(_?: string, cache = new JSONCacheMap()): DefaultObject {
    // ...
    const result: DefaultObject = {};
    // Implementation details...
    return result;
  }

  // ...
}
  

Source:

Object.ts#L383-L410

Documentation Reference:

RealmObject.toJSON

Suggested Change:

It might be more accurate for the toJSON method to return a type T to reflect the actual data structure of the RealmObject instance. This change would align the method's return type with the generic type used in the class definition.

Could you please review this and consider updating the TypeScript definition if appropriate? Thank you for looking into this matter.

Copy link

sync-by-unito bot commented Jun 17, 2024

➤ PM Bot commented:

Jira ticket: RJS-2838

@CacaoRick
Copy link
Author

If OmittedRealmTypes can remove model properties from a Realm Object,
perhaps it should return OmittedRealmTypes<T>?

@vinibgoulart vinibgoulart linked a pull request Jul 15, 2024 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant