TYpes: better type 'On'

This commit is contained in:
Pieter Vander Vennet 2025-07-08 03:38:12 +02:00
parent f5888dee56
commit cb0cb710a9
3 changed files with 11 additions and 10 deletions

View file

@ -166,11 +166,11 @@ export class Each<X, Y> extends Conversion<X[], Y[]> {
} }
} }
export class On<P, T> extends DesugaringStep<T> { export class On<S extends string, P, T extends Record<S, P>> extends DesugaringStep<T> {
private readonly key: string private readonly key: S
private readonly step: (t: T) => Conversion<P, P> private readonly step: (t: T) => Conversion<P, P>
constructor(key: string, step: Conversion<P, P> | ((t: T) => Conversion<P, P>)) { constructor(key: S, step: Conversion<P, P> | ((t: T) => Conversion<P, P>)) {
super(`On(${key}, ${step.name})`, "Applies " + step.name + " onto property `" + key + "`") super(`On(${key}, ${step.name})`, "Applies " + step.name + " onto property `" + key + "`")
if (typeof step === "function") { if (typeof step === "function") {
this.step = step this.step = step
@ -181,7 +181,7 @@ export class On<P, T> extends DesugaringStep<T> {
} }
convert(json: T, context: ConversionContext): T { convert(json: T, context: ConversionContext): T {
const key = this.key const key: S = this.key
const value: P = json?.[key] const value: P = json?.[key]
if (value === undefined || value === null) { if (value === undefined || value === null) {
return json return json
@ -189,7 +189,8 @@ export class On<P, T> extends DesugaringStep<T> {
json = { ...json } json = { ...json }
const step = this.step(json) const step = this.step(json)
json[key] = step.convert(value, context.enter(key).inOperation("on[" + key + "]")) const converted = step.convert(value, context.enter(key).inOperation("on[" + key + "]"))
json[key] = <T[S]> converted
return json return json
} }
} }

View file

@ -329,15 +329,15 @@ export class PrevalidateLayer extends DesugaringStep<LayerConfigJson> {
} }
} }
if (json.filter) { if (json.filter && Array.isArray(json.filter)) {
new On("filter", new Each(new ValidateFilter())).convert(json, context) new On("filter", new Each(new ValidateFilter())).convert(<any> json, context)
} }
if (json.tagRenderings !== undefined) { if (json.tagRenderings !== undefined) {
new On( new On(
"tagRenderings", "tagRenderings",
new Each(new ValidateTagRenderings(json, this._doesImageExist)) new Each(new ValidateTagRenderings(json, this._doesImageExist))
).convert(json, context) ).convert(<any> json, context)
} }
if (json.pointRendering !== null && json.pointRendering !== undefined) { if (json.pointRendering !== null && json.pointRendering !== undefined) {

View file

@ -901,12 +901,12 @@ export class ValidateLayer extends Conversion<
} }
} }
export class ValidateFilter extends DesugaringStep<FilterConfigJson> { export class ValidateFilter extends DesugaringStep<string | FilterConfigJson> {
constructor() { constructor() {
super("ValidateFilter", "Detect common errors in the filters") super("ValidateFilter", "Detect common errors in the filters")
} }
convert(filter: FilterConfigJson, context: ConversionContext): FilterConfigJson { convert(filter: string | FilterConfigJson, context: ConversionContext): string | FilterConfigJson {
if (typeof filter === "string") { if (typeof filter === "string") {
// Calling another filter, we skip // Calling another filter, we skip
return filter return filter