From 130afe7b23ae49f422ea52cc7d1e783517a8a519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Thu, 23 May 2024 12:15:53 +0200 Subject: [PATCH] Adding types for passing objects when constructing objects --- packages/realm/src/Object.ts | 2 +- packages/realm/src/Unmanaged.ts | 14 +++++++++++++- packages/realm/src/schema/types.ts | 5 +++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/realm/src/Object.ts b/packages/realm/src/Object.ts index 51135039f5..86681ff6fc 100644 --- a/packages/realm/src/Object.ts +++ b/packages/realm/src/Object.ts @@ -74,7 +74,7 @@ type CreationContext = { }; // eslint-disable-next-line @typescript-eslint/no-explicit-any -export type AnyRealmObject = RealmObject; +export type AnyRealmObject = RealmObject; export const KEY_ARRAY = Symbol("Object#keys"); export const KEY_SET = Symbol("Object#keySet"); diff --git a/packages/realm/src/Unmanaged.ts b/packages/realm/src/Unmanaged.ts index e8af6fa278..879e98807d 100644 --- a/packages/realm/src/Unmanaged.ts +++ b/packages/realm/src/Unmanaged.ts @@ -45,12 +45,22 @@ type RealmDictionaryRemappedModelPart = { : never; }; +/** + * Exchanges properties defined as {@link Realm.Object} with their unmanaged version. + */ +type RealmObjectRemappedModelPart = { + [K in ExtractPropertyNamesOfType]?: T[K] extends Realm.Object + ? T[K] | Unmanaged + : never; +}; + /** Omits all properties of a model which are not defined by the schema */ export type OmittedRealmTypes = Omit< T, | keyof AnyRealmObject /* eslint-disable-next-line @typescript-eslint/ban-types */ | ExtractPropertyNamesOfType // TODO: Figure out the use-case for this + | ExtractPropertyNamesOfType | ExtractPropertyNamesOfType | ExtractPropertyNamesOfType >; @@ -68,7 +78,9 @@ type OmittedRealmTypesWithRequired; /** Remaps realm types to "simpler" types (arrays and objects) */ -type RemappedRealmTypes = RealmListsRemappedModelPart & RealmDictionaryRemappedModelPart; +type RemappedRealmTypes = RealmListsRemappedModelPart & + RealmDictionaryRemappedModelPart & + RealmObjectRemappedModelPart; /** * Joins `T` stripped of all keys which value extends {@link Collection} and all inherited from {@link Realm.Object}, diff --git a/packages/realm/src/schema/types.ts b/packages/realm/src/schema/types.ts index c4a3160d39..eb19ad0f90 100644 --- a/packages/realm/src/schema/types.ts +++ b/packages/realm/src/schema/types.ts @@ -21,9 +21,10 @@ import { BSON, Realm, RealmObject } from "../internal"; export type DefaultObject = Record; /* eslint-disable-next-line @typescript-eslint/no-explicit-any -- Using `any` instead of `unknown` here to make it easier to pass */ export type Constructor = { new (...args: any): T }; -export type RealmObjectConstructor = { +/* eslint-disable-next-line @typescript-eslint/no-explicit-any -- Using `any` instead of `unknown` here to make it easier to pass */ +export type RealmObjectConstructor = { /* eslint-disable-next-line @typescript-eslint/no-explicit-any -- Using `any` instead of `unknown` here to make it easier to pass */ - new (...args: any): T; + new (...args: Args): T; // We need to declare schema as optional to support the babel plugin. // Otherwise it will produce a type error. schema?: ObjectSchema;