Skip to content

Commit

Permalink
Make sure to include DataModel in the inheritance chain of TypeDataMo…
Browse files Browse the repository at this point in the history
…del explicitly
  • Loading branch information
LukeAbby committed Dec 22, 2024
1 parent a837473 commit ac6ea72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/foundry/common/abstract/type-data.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ import type BaseUser from "../documents/user.d.mts";
import type { DataModel } from "./data.d.mts";
import type Document from "./document.d.mts";

type StaticDataModel = typeof DataModel<DataSchema, Document<any, DataSchema, any>>;
declare class AnyDataModel extends DataModel<any, any, any> {
constructor(...args: any[]);
}

type StaticDataModel = typeof AnyDataModel;

interface _InternalTypeDataModelInterface extends StaticDataModel {
new <Schema extends DataSchema, Parent extends Document.Any, _ComputedInstance extends DataModel<Schema, Parent>>(
...args: ConstructorParameters<typeof DataModel<Schema, Parent>>
): _ComputedInstance;

// Note(LukeAbby): This seemingly redundant `DataModel<Schema, Parent>` is to
// Ensure that TypeScript allows overriding `protected` methods in subclasses.
// See: https://gist.github.com/LukeAbby/b9fd57eeba778a25297721e88b3e6bdd
): DataModel<Schema, Parent> & _ComputedInstance;
}

declare const _InternalTypeDataModelConst: _InternalTypeDataModelInterface;
Expand Down
6 changes: 6 additions & 0 deletions tests/foundry/common/abstract/type-data.mjs.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ type DerivedQuestData = { totalSteps: number };
class QuestModel extends TypeDataModel<QuestSchema, BaseJournalEntryPage, BaseQuestData, DerivedQuestData> {
otherMethod() {}

// This override may seem random but it's a regression test for this error:
// Class 'QuestModel' incorrectly extends base class 'TypeDataModel<QuestSchema, BaseJournalEntryPage, BaseQuestData, DerivedQuestData>'.
// Property '_initialize' is protected but type 'QuestModel' is not a class derived from 'DataModel<Schema, Parent, ExtraConstructorOptions>'.
// See: https://gist.github.com/LukeAbby/b9fd57eeba778a25297721e88b3e6bdd
override _initialize(): void {}

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

Expand Down

0 comments on commit ac6ea72

Please sign in to comment.