foundry-vtt-types icon indicating copy to clipboard operation
foundry-vtt-types copied to clipboard

Counter-example for typing `DataField#initial` as `(source: DataModel.Any) => InitializedType`

Open LukeAbby opened this issue 1 year ago • 2 comments
trafficstars

So far I've only identified one place that a DataField#initial is called with an argument and that is in EmbeddedCollectionField#_cleanType:

return value.map(v => this.schema.clean(v, {...options, source: v}));

this.schema is a SchemaField so this calls SchemaField#clean (no specific override here) so it goes to DataField#clean which has this line:

if ( value === undefined ) return this.getInitialValue(options.source);

and getInitialValue is:

  getInitialValue(data) {
    return this.initial instanceof Function ? this.initial(data) : this.initial;
  }

Therefore DataField#initial is called with source: v which will be some Document. I've typed DataField#initial based off of this but there may be counterexamples out there.

LukeAbby avatar Aug 05 '24 07:08 LukeAbby

Did regex for clean.*source

  • EmbeddedDataField passes value, which would be the source data for the embedded model I believe?
  • EmbeddedCollectionDeltaField is configured similarly to EmbeddedCollectionField but sometimes deferring to TombstoneData rather than the model
  • TypeDataField similarly calls cls.cleanData(value, {...options, source: value});

I think though in each of these cases we're dealing with the json-serializable _source data rather than the model instance itself.

JPMeehan avatar Aug 05 '24 15:08 JPMeehan

Update from discord - looks like it's just AnyObject getting passed through. Only example of actual function-based usage was sight.enabled in Token

JPMeehan avatar Aug 06 '24 07:08 JPMeehan