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

Make counter types requirable #6732

Draft
wants to merge 3 commits 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
3 changes: 2 additions & 1 deletion packages/realm/src/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
JSONCacheMap,
ObjectChangeCallback,
ObjectListeners,
OmittedRealmTypes,

Check failure on line 30 in packages/realm/src/Object.ts

View workflow job for this annotation

GitHub Actions / Lint

'OmittedRealmTypes' is defined but never used
OrderedCollection,
Realm,
RealmObjectConstructor,
RequirableProperties,
Results,
TypeAssertionError,
TypeHelpers,
Expand Down Expand Up @@ -144,7 +145,7 @@
* }
* ```
* @see {@link ObjectSchema}
* @typeParam `T` - The type of this class (e.g. if your class is `Person`,

Check warning on line 148 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 All @@ -152,7 +153,7 @@
* properties not specified will be optional, and will default to a sensible
* null value if no default is specified elsewhere.
*/
export class RealmObject<T = DefaultObject, RequiredProperties extends keyof OmittedRealmTypes<T> = never> {
export class RealmObject<T = DefaultObject, RequiredProperties extends keyof RequirableProperties<T> = never> {
/**
* This property is stored on the per class prototype when transforming the schema.
* @internal
Expand Down
34 changes: 22 additions & 12 deletions packages/realm/src/Unmanaged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@
/**
* Exchanges properties defined as a {@link Counter} with a `number`.
*/
type RealmCounterRemappedModelPart<T> = {
[K in ExtractPropertyNamesOfTypeExcludingNullability<T, Counter>]?: Counter | number | Exclude<T[K], Counter>;
};
type RealmCounterRemappedModelPart<T, RequiredProperties extends keyof RequirableProperties<T>> = OptionalExceptRequired<T, {

Check failure on line 64 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `T,·RequiredProperties·extends·keyof·RequirableProperties<T>>·=·OptionalExceptRequired<T,` with `⏎··T,⏎··RequiredProperties·extends·keyof·RequirableProperties<T>,⏎>·=·OptionalExceptRequired<⏎··T,⏎·`
[K in ExtractPropertyNamesOfTypeExcludingNullability<T, Counter>]: Counter | number | Exclude<T[K], Counter>;

Check failure on line 65 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `··`
}, RequiredProperties>;

Check failure on line 66 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `},·RequiredProperties` with `··},⏎··RequiredProperties⏎`

type RealmObjectRemappedModelPart<T, RequiredProperties extends keyof RequirableProperties<T>> = OptionalExceptRequired<T, {

Check failure on line 68 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `T,` with `⏎··T,⏎·`
[K in ExtractPropertyNamesOfTypeExcludingNullability<T, Realm.Object<any>>]: Unmanaged<T[K]> | undefined | null;

Check failure on line 69 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `··`

Check failure on line 69 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
}, RequiredProperties>;

Check failure on line 70 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `},·RequiredProperties` with `··},⏎··RequiredProperties⏎`

/** Omits all properties of a model which are not defined by the schema */
export type OmittedRealmTypes<T> = Omit<
Expand All @@ -74,33 +78,39 @@
| ExtractPropertyNamesOfType<T, AnyCollection>
| ExtractPropertyNamesOfType<T, AnyDictionary>
| ExtractPropertyNamesOfTypeExcludingNullability<T, Counter>
| ExtractPropertyNamesOfTypeExcludingNullability<T, Realm.Object<any>>

Check failure on line 81 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
>;

/** Make all fields optional except those specified in K */
type OptionalExcept<T, K extends keyof T> = Partial<T> & Pick<T, K>;
export type RequirableProperties<T> = Omit<T, keyof AnyRealmObject>;

/** Make all fields optional except those specified in RequiredProperties */
type OptionalExceptRequired<T, TPart, RequiredProperties extends keyof RequirableProperties<T>> = Partial<TPart> & Pick<TPart, RequiredProperties & keyof TPart>;

Check failure on line 87 in packages/realm/src/Unmanaged.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎·`

/**
* Omits all properties of a model which are not defined by the schema,
* making all properties optional except those specified in RequiredProperties.
*/
type OmittedRealmTypesWithRequired<T, RequiredProperties extends keyof OmittedRealmTypes<T>> = OptionalExcept<
type OmittedRealmTypesWithRequired<T, RequiredProperties extends keyof RequirableProperties<T>> = OptionalExceptRequired<
T,
OmittedRealmTypes<T>,
RequiredProperties
>;

/** Remaps realm types to "simpler" types (arrays and objects) */
type RemappedRealmTypes<T> = RealmListRemappedModelPart<T> &
RealmDictionaryRemappedModelPart<T> &
RealmSetRemappedModelPart<T> &
RealmCounterRemappedModelPart<T>;
type RemappedRealmTypes<T, RequiredProperties extends keyof RequirableProperties<T>> =
& RealmListRemappedModelPart<T>
& RealmDictionaryRemappedModelPart<T>
& RealmSetRemappedModelPart<T>
& RealmCounterRemappedModelPart<T, RequiredProperties>
& RealmObjectRemappedModelPart<T, RequiredProperties>;

/**
* Joins `T` stripped of all keys which value extends {@link Collection} and all inherited from {@link Realm.Object},
* with only the keys which value extends {@link List}, remapped as {@link Array}. All properties are optional
* except those specified in `RequiredProperties`.
*/
export type Unmanaged<T, RequiredProperties extends keyof OmittedRealmTypes<T> = never> = OmittedRealmTypesWithRequired<
export type Unmanaged<T, RequiredProperties extends keyof RequirableProperties<T> = never> = OmittedRealmTypesWithRequired<
T,
RequiredProperties
> &
RemappedRealmTypes<T>;
RemappedRealmTypes<T, RequiredProperties>;
Loading