Skip to content

Commit

Permalink
Update return type of 'assert.never'. (#5718)
Browse files Browse the repository at this point in the history
  • Loading branch information
elle-j authored Apr 13, 2023
1 parent 1d232b0 commit 9bdd7cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/realm/src/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export class RealmObject<T = DefaultObject> {
} else if (value instanceof BSON.UUID) {
return "uuid";
} else {
throw assert.never(value, "value");
assert.never(value, "value");
}
} else {
return typeName;
Expand Down
4 changes: 2 additions & 2 deletions packages/realm/src/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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");
}
}
}
Expand Down
29 changes: 17 additions & 12 deletions packages/realm/src/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <K extends string | number | symbol = string, V = unknown>(
value: unknown,
target?: string,
Expand Down Expand Up @@ -133,12 +122,28 @@ assert.iterable = (value: unknown, target?: string): asserts value is Iterable<u
assert(Symbol.iterator in value, () => 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.");
};
Expand Down

0 comments on commit 9bdd7cc

Please sign in to comment.