Skip to content

@vttforge/core / BaseTypeDataModel

Function: BaseTypeDataModel()

Call Signature

BaseTypeDataModel(): (...args) => TypeDataModelHooks

Build a base class with no knowledge of the schema.

The hooks are typed; the schema's own fields are not, since nothing said what they are. Pass your schema function instead to get those too.

Returns

(...args) => TypeDataModelHooks

Call Signature

BaseTypeDataModel<S>(defineSchema): TypedTypeDataModelCtor<S>

Build a base class that knows its schema.

Hand it the function that returns your fields and it implements static defineSchema() for you, so the schema is written once:

ts
const defineCharacterSchema = () => {
  const f = fields();
  return { level: new f.NumberField({ required: true, nullable: false, initial: 1 }) };
};

class CharacterData extends BaseTypeDataModel(defineCharacterSchema) {
  declare armorClass: number;
  prepareDerivedData() {
    this.armorClass = 10 + this.level; // this.level is number
  }
}

It has to be a function, not an object: fields() reads a Foundry global that does not exist when the module is first evaluated.

A subclass may still declare its own static defineSchema(); that one wins, the same as any other static.

Type Parameters

S

S extends Record<string, FieldInstance>

Parameters

defineSchema

() => S

Returns

TypedTypeDataModelCtor<S>

MIT licensed.