Skip to content

Commit

Permalink
Class-based Models types (#4537)
Browse files Browse the repository at this point in the history
* Improving ClassModel type

* Fixed changelog
  • Loading branch information
kraenhansen committed Jul 7, 2022
1 parent 9cdb7d9 commit f451f98
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 3 additions & 3 deletions integration-tests/tests/src/tests/class-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("Class models", () => {
});

it("is allowed", () => {
class Person extends Realm.Object {
class Person extends Realm.Object<Person> {
name!: string;
static schema: Realm.ObjectSchema = {
name: "Person",
Expand All @@ -75,8 +75,8 @@ describe("Class models", () => {
});

describe("#constructor", () => {
type UnmanagedPerson = Partial<Person> & Pick<Person, "name">;
class Person extends Realm.Object<UnmanagedPerson> {
// The Pick and Partial is needed to correctly reflect the defaults
class Person extends Realm.Object<Pick<Person, "name"> & Partial<Person>> {
id!: Realm.BSON.ObjectId;
name!: string;
age!: number;
Expand Down
9 changes: 5 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ declare namespace Realm {
* ObjectClass
* @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~ObjectClass }
*/
interface ObjectClass {
schema: ObjectSchema;
type ObjectClass<T extends Realm.Object<T> = any> = {
new(...args: any): Realm.Object<T>;
schema?: ObjectSchema;
}

type PrimaryKey = number | string | Realm.BSON.ObjectId | Realm.BSON.UUID;
Expand Down Expand Up @@ -324,9 +325,9 @@ declare namespace Realm {
/**
* @returns void
*/
addListener(callback: ObjectChangeCallback<this>): void;
addListener(callback: ObjectChangeCallback<T>): void;

removeListener(callback: ObjectChangeCallback<this>): void;
removeListener(callback: ObjectChangeCallback<T>): void;

removeAllListeners(): void;

Expand Down

0 comments on commit f451f98

Please sign in to comment.