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 types for passing objects when constructing objects #6675

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/realm/src/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyRealmObject = RealmObject<any>;
export type AnyRealmObject = RealmObject<any, any>;

export const KEY_ARRAY = Symbol("Object#keys");
export const KEY_SET = Symbol("Object#keySet");
Expand Down Expand Up @@ -141,7 +141,7 @@
* }
* ```
* @see {@link ObjectSchema}
* @typeParam `T` - The type of this class (e.g. if your class is `Person`,

Check warning on line 144 in packages/realm/src/Object.ts

View workflow job for this annotation

GitHub Actions / Lint

The type 'ObjectSchema' is undefined
* `T` should also be `Person` - this duplication is required due to how
* TypeScript works)
* @typeParam `RequiredProperties` - The names of any properties of this
Expand Down
14 changes: 13 additions & 1 deletion packages/realm/src/Unmanaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,22 @@ type RealmDictionaryRemappedModelPart<T> = {
: never;
};

/**
* Exchanges properties defined as {@link Realm.Object} with their unmanaged version.
*/
type RealmObjectRemappedModelPart<T> = {
[K in ExtractPropertyNamesOfType<T, AnyRealmObject>]?: T[K] extends Realm.Object<infer Type>
? T[K] | Unmanaged<Type>
: never;
};

/** Omits all properties of a model which are not defined by the schema */
export type OmittedRealmTypes<T> = Omit<
T,
| keyof AnyRealmObject
/* eslint-disable-next-line @typescript-eslint/ban-types */
| ExtractPropertyNamesOfType<T, Function> // TODO: Figure out the use-case for this
| ExtractPropertyNamesOfType<T, AnyRealmObject>
| ExtractPropertyNamesOfType<T, AnyCollection>
| ExtractPropertyNamesOfType<T, AnyDictionary>
>;
Expand All @@ -68,7 +78,9 @@ type OmittedRealmTypesWithRequired<T, RequiredProperties extends keyof OmittedRe
>;

/** Remaps realm types to "simpler" types (arrays and objects) */
type RemappedRealmTypes<T> = RealmListsRemappedModelPart<T> & RealmDictionaryRemappedModelPart<T>;
type RemappedRealmTypes<T> = RealmListsRemappedModelPart<T> &
RealmDictionaryRemappedModelPart<T> &
RealmObjectRemappedModelPart<T>;

/**
* Joins `T` stripped of all keys which value extends {@link Collection} and all inherited from {@link Realm.Object},
Expand Down
5 changes: 3 additions & 2 deletions packages/realm/src/schema/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import { BSON, Realm, RealmObject } from "../internal";
export type DefaultObject = Record<string, unknown>;
/* eslint-disable-next-line @typescript-eslint/no-explicit-any -- Using `any` instead of `unknown` here to make it easier to pass */
export type Constructor<T = unknown> = { new (...args: any): T };
export type RealmObjectConstructor<T extends RealmObject = RealmObject> = {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any -- Using `any` instead of `unknown` here to make it easier to pass */
export type RealmObjectConstructor<T extends RealmObject = RealmObject, Args extends unknown[] = any[]> = {
/* 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;
Expand Down
Loading