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

fix(toJSON): get the generic type T for toJSON response #6789

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/realm/src/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,9 @@ export class RealmObject<T = DefaultObject, RequiredProperties extends keyof Omi
* and [flatted](https://www.npmjs.com/package/flatted) to stringify Realm entities that have circular structures.
* @returns A plain object.
*/
toJSON(_?: string, cache?: unknown): OmittedRealmTypes<T>;
toJSON(_?: string, cache?: unknown): OmittedRealmTypes<T | Unmanaged<T>>;
/** @internal */
toJSON(_?: string, cache = new JSONCacheMap<T>()): OmittedRealmTypes<T> {
toJSON(_?: string, cache = new JSONCacheMap<T>()): OmittedRealmTypes<T | Unmanaged<T>> {
Copy link

@CacaoRick CacaoRick Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your commit.

Unmanaged<T> will remap all properties to optional.

If a property is not optional in its Realm.Object schema, instances created by that Object will certainly have that property and it will not be optional.

Will this cause the object returned by toJSON() to need to be remapped with type definitions in order to be used correctly?

Maybe OmittedRealmTypes<T> is sufficient?

// Construct a reference-id of table-name & primaryKey if it exists, or fall back to objectId.

// Check if current objectId has already processed, to keep object references the same.
Expand Down