diff --git a/packages/realm/src/Object.ts b/packages/realm/src/Object.ts index 1861323bf4..9e13a8ace9 100644 --- a/packages/realm/src/Object.ts +++ b/packages/realm/src/Object.ts @@ -478,7 +478,7 @@ export class RealmObject { } else if (value instanceof BSON.UUID) { return "uuid"; } else { - throw assert.never(value, "value"); + assert.never(value, "value"); } } else { return typeName; diff --git a/packages/realm/src/Realm.ts b/packages/realm/src/Realm.ts index 02ea5fbc24..e84b2caacb 100644 --- a/packages/realm/src/Realm.ts +++ b/packages/realm/src/Realm.ts @@ -1045,7 +1045,7 @@ export class Realm { } else if (eventName === RealmEvent.BeforeNotify) { this.beforeNotifyListeners.remove(callback); } else { - throw assert.never(eventName, "eventName"); + assert.never(eventName, "eventName"); } } @@ -1070,7 +1070,7 @@ export class Realm { } else if (eventName === RealmEvent.BeforeNotify) { this.beforeNotifyListeners.removeAll(); } else { - throw assert.never(eventName, "eventName"); + assert.never(eventName, "eventName"); } } } diff --git a/packages/realm/src/assert.ts b/packages/realm/src/assert.ts index 495ac4f2b3..b2bffef453 100644 --- a/packages/realm/src/assert.ts +++ b/packages/realm/src/assert.ts @@ -81,17 +81,6 @@ assert.symbol = (value: unknown, target?: string): asserts value is symbol => { assert(typeof value === "symbol", () => new TypeAssertionError("a symbol", value, target)); }; -assert.primaryKey = (value: unknown, target?: string): asserts value is PrimaryKey => { - assert( - value === null || - typeof value === "number" || - typeof value === "string" || - value instanceof BSON.UUID || - value instanceof BSON.ObjectId, - () => new TypeAssertionError("a primary key", value, target), - ); -}; - assert.object = ( value: unknown, target?: string, @@ -133,12 +122,28 @@ assert.iterable = (value: unknown, target?: string): asserts value is Iterable new TypeAssertionError("iterable", value, target)); }; -assert.never = (value: never, target?: string): asserts value is never => { +// * Use arg type `value: never` rather than `value: unknown` to get a compile time +// error when e.g. not including if-checks for all enum values. +// * Use return type `never` rather than `asserts value is never` to remove the +// need for callers to explicitly throw (i.e. `throw assert.never()`) as a way +// for TS to detect unreachable code. +assert.never = (value: never, target?: string): never => { throw new TypeAssertionError("never", value, target); }; // SDK specific +assert.primaryKey = (value: unknown, target?: string): asserts value is PrimaryKey => { + assert( + value === null || + typeof value === "number" || + typeof value === "string" || + value instanceof BSON.UUID || + value instanceof BSON.ObjectId, + () => new TypeAssertionError("a primary key", value, target), + ); +}; + assert.open = (realm: Realm) => { assert(!realm.isClosed, "Cannot access realm that has been closed."); };