Skip to content

Commit

Permalink
Minor TypeDataModel fix
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeAbby committed Oct 19, 2024
1 parent dc12f72 commit 856c5e7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/foundry/common/abstract/type-data.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ declare namespace TypeDataModel {
* }
* ```
*/
export default abstract class TypeDataModel<
declare abstract class TypeDataModel<
Schema extends DataSchema,
Parent extends Document.Any,
BaseData extends AnyObject = EmptyObject,
Expand Down Expand Up @@ -333,3 +333,5 @@ export default abstract class TypeDataModel<
*/
protected _onDelete(options: Document.OnDeleteOptions<any>, userId: string): void;
}

export default TypeDataModel;
42 changes: 19 additions & 23 deletions tests/foundry/common/abstract/type-data.mjs.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { expectTypeOf } from "vitest";
import type { fields } from "../../../../src/foundry/common/data/module.d.mts";
import type BaseJournalEntryPage from "../../../../src/foundry/common/documents/journal-entry-page.d.mts";
import type { TypeDataModel } from "../../../../src/foundry/common/abstract/type-data.d.mts";
import type BaseUser from "../../../../src/foundry/common/documents/user.d.mts";
import type { DeepPartial } from "../../../../src/types/utils.d.mts";
import type { DeepPartial, EmptyObject } from "../../../../src/types/utils.d.mts";

import TypeDataModel = foundry.abstract.TypeDataModel;

/* attempting to use the example as a test */

Expand All @@ -24,20 +25,15 @@ type DerivedQuestData = { totalSteps: number };

// Test With specified Base and DerivedData.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class QuestModel extends foundry.abstract.TypeDataModel<
QuestSchema,
BaseJournalEntryPage,
BaseQuestData,
DerivedQuestData
> {
class QuestModel extends TypeDataModel<QuestSchema, BaseJournalEntryPage, BaseQuestData, DerivedQuestData> {
otherMethod() {}

prepareBaseData(this: TypeDataModel.PrepareBaseDataThis<this>): void {
override prepareBaseData(this: TypeDataModel.PrepareBaseDataThis<this>): void {
this.otherMethod();

// From schema
expectTypeOf(this.steps).toEqualTypeOf<string[]>;
expectTypeOf(this.description).toEqualTypeOf<HTMLElement | undefined>;
expectTypeOf(this.description).toEqualTypeOf<Partial<HTMLElement> | undefined>;

// @ts-expect-error Derived Data is not available yet
this.totalSteps + 1;
Expand All @@ -49,11 +45,11 @@ class QuestModel extends foundry.abstract.TypeDataModel<
this.prepareDerivedData();
}

prepareDerivedData(this: TypeDataModel.PrepareDerivedDataThis<this>): void {
override prepareDerivedData(this: TypeDataModel.PrepareDerivedDataThis<this>): void {
this.otherMethod();

// From JournalEntryPage
expectTypeOf(this.flags).toEqualTypeOf<Record<string, unknown>>;
expectTypeOf(this.flags).toEqualTypeOf<EmptyObject>;

// From QuestSchema
expectTypeOf(this.steps).toEqualTypeOf<string[]>;
Expand All @@ -73,7 +69,7 @@ class QuestModel extends foundry.abstract.TypeDataModel<
this.prepareDerivedData();
}

protected async _preCreate(
protected override async _preCreate(
data: TypeDataModel.ParentAssignmentType<this>,
options: TypeDataModel.TypeDataModelModificationOptions,
user: BaseUser,
Expand All @@ -84,7 +80,7 @@ class QuestModel extends foundry.abstract.TypeDataModel<
expectTypeOf(user).toEqualTypeOf<BaseUser>();
}

protected async _preUpdate(
protected override async _preUpdate(
data: DeepPartial<TypeDataModel.ParentAssignmentType<this>>,
options: TypeDataModel.TypeDataModelModificationOptions,
userId: string,
Expand All @@ -99,9 +95,9 @@ class QuestModel extends foundry.abstract.TypeDataModel<
/* Test with default BaseData and DerivedData */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class QuestModel2 extends foundry.abstract.TypeDataModel<QuestSchema, BaseJournalEntryPage> {
prepareBaseData(this: TypeDataModel.PrepareBaseDataThis<this>): void {
override prepareBaseData(this: TypeDataModel.PrepareBaseDataThis<this>): void {
// From JournalEntryPage
expectTypeOf(this.flags).toEqualTypeOf<Record<string, unknown>>;
expectTypeOf(this.flags).toEqualTypeOf<EmptyObject>;

// Comes from the schema
expectTypeOf(this.steps).toEqualTypeOf<string[]>;
Expand All @@ -113,9 +109,9 @@ class QuestModel2 extends foundry.abstract.TypeDataModel<QuestSchema, BaseJourna
this.totalSteps + 1;
}

prepareDerivedData(this: TypeDataModel.PrepareDerivedDataThis<this>): void {
override prepareDerivedData(this: TypeDataModel.PrepareDerivedDataThis<this>): void {
// From JournalEntryPage
expectTypeOf(this.flags).toEqualTypeOf<Record<string, unknown>>;
expectTypeOf(this.flags).toEqualTypeOf<EmptyObject>;

// Comes from the schema
expectTypeOf(this.steps).toEqualTypeOf<string[]>;
Expand All @@ -131,21 +127,21 @@ class QuestModel2 extends foundry.abstract.TypeDataModel<QuestSchema, BaseJourna
/* Test with no DerivedData */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class QuestModel3 extends foundry.abstract.TypeDataModel<QuestSchema, BaseJournalEntryPage, BaseQuestData> {
prepareBaseData(this: TypeDataModel.PrepareBaseDataThis<this>): void {
override prepareBaseData(this: TypeDataModel.PrepareBaseDataThis<this>): void {
// From JournalEntryPage
expectTypeOf(this.flags).toEqualTypeOf<Record<string, unknown>>;
expectTypeOf(this.flags).toEqualTypeOf<EmptyObject>;

// From BaseData
expectTypeOf(this.steps).toEqualTypeOf<string[]>;
expectTypeOf(this.description).toEqualTypeOf<HTMLElement | undefined>;
expectTypeOf(this.description).toEqualTypeOf<Partial<HTMLElement> | undefined>;

// @ts-expect-error There is no derived Data
this.totalSteps + 1;
}

prepareDerivedData(this: TypeDataModel.PrepareDerivedDataThis<this>): void {
override prepareDerivedData(this: TypeDataModel.PrepareDerivedDataThis<this>): void {
// From JournalEntryPage
expectTypeOf(this.flags).toEqualTypeOf<Record<string, unknown>>;
expectTypeOf(this.flags).toEqualTypeOf<EmptyObject>;

// From BaseData
expectTypeOf(this.steps).toEqualTypeOf<string[]>;
Expand Down
11 changes: 5 additions & 6 deletions tests/foundry/common/documents/actor.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expectTypeOf } from "vitest";
import type EmbeddedCollection from "../../../../src/foundry/common/abstract/embedded-collection.d.mts";
import type { NumberField, SchemaField } from "../../../../src/foundry/common/data/fields.d.mts";
import type { DataModel } from "../../../../src/foundry/common/abstract/data.d.mts";
import type { AnyMutableObject, AnyObject, EmptyObject, Merge } from "../../../../src/types/utils.d.mts";
import type { AnyMutableObject, AnyObject, EmptyObject } from "../../../../src/types/utils.d.mts";

import TypeDataModel = foundry.abstract.TypeDataModel;

// @ts-expect-error name and type are required
new foundry.documents.BaseActor();
Expand Down Expand Up @@ -47,7 +48,7 @@ type MyCharacterSchema = {
}>;
};

class MyCharacter extends foundry.abstract.TypeDataModel<MyCharacterSchema, Actor.ConfiguredInstance> {
class MyCharacter extends TypeDataModel<MyCharacterSchema, Actor.ConfiguredInstance> {
static override defineSchema() {
const { SchemaField, NumberField } = foundry.data.fields;
return {
Expand All @@ -70,7 +71,7 @@ class MyCharacter extends foundry.abstract.TypeDataModel<MyCharacterSchema, Acto
};
}

override prepareDerivedData(this: Merge<DataModel<MyCharacterSchema, Actor.ConfiguredInstance>, EmptyObject>): void {
override prepareDerivedData(this: TypeDataModel.PrepareDerivedDataThis<this>): void {
this.abilities.strength.value + 2;
for (const ability of Object.values(this.abilities)) {
// @ts-expect-error Derived data must be declared
Expand All @@ -79,8 +80,6 @@ class MyCharacter extends foundry.abstract.TypeDataModel<MyCharacterSchema, Acto
}
}

// interface MyCharacter extends foundry.data.fields.SchemaField.InnerInitializedType<MyCharacterSchema> {}

declare const MyCharacterSystem: MyCharacter;

expectTypeOf(MyCharacterSystem.abilities.strength.value).toEqualTypeOf<number>();
Expand Down

0 comments on commit 856c5e7

Please sign in to comment.