From f565b9ee6e2311a67997a90520e5f726e35ed0a0 Mon Sep 17 00:00:00 2001 From: Pax1601 Date: Tue, 21 Oct 2025 17:34:20 +0200 Subject: [PATCH] Add type annotations and key conversions in Map class Improves type safety by adding explicit type annotations to method parameters and callback functions in the Map class. Updates key handling for object properties to ensure correct types, particularly when interacting with ContextActions, MapOptions, MapHiddenTypes, and destination preview markers. --- frontend/react/src/map/map.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/frontend/react/src/map/map.ts b/frontend/react/src/map/map.ts index bb34eb8a..ed3cdacc 100644 --- a/frontend/react/src/map/map.ts +++ b/frontend/react/src/map/map.ts @@ -443,7 +443,7 @@ export class Map extends L.Map { ctrlKey: false, }); - for (let contextActionName in ContextActions) { + for (const contextActionName of Object.keys(ContextActions) as Array) { const contextAction = ContextActions[contextActionName] as ContextAction; if (contextAction.getOptions().code) { getApp() @@ -631,13 +631,13 @@ export class Map extends L.Map { return this.#spawnHeading; } - addStarredSpawnRequestTable(key, spawnRequestTable: SpawnRequestTable, quickAccessName: string) { + addStarredSpawnRequestTable(key: string, spawnRequestTable: SpawnRequestTable, quickAccessName: string) { this.#starredSpawnRequestTables[key] = spawnRequestTable; this.#starredSpawnRequestTables[key].quickAccessName = quickAccessName; StarredSpawnsChangedEvent.dispatch(this.#starredSpawnRequestTables); } - removeStarredSpawnRequestTable(key) { + removeStarredSpawnRequestTable(key: string) { if (key in this.#starredSpawnRequestTables) delete this.#starredSpawnRequestTables[key]; StarredSpawnsChangedEvent.dispatch(this.#starredSpawnRequestTables); } @@ -678,7 +678,7 @@ export class Map extends L.Map { } setHiddenType(key: string, value: boolean) { - this.#hiddenTypes[key] = value; + this.#hiddenTypes[key as keyof MapHiddenTypes] = value; HiddenTypesChangedEvent.dispatch(this.#hiddenTypes); } @@ -788,13 +788,13 @@ export class Map extends L.Map { return smokeMarker; } - setOption(key, value) { + setOption(key: K, value: MapOptions[K]) { this.#options[key] = value; - MapOptionsChangedEvent.dispatch(this.#options, key); + MapOptionsChangedEvent.dispatch(this.#options, key as keyof MapOptions); } - setOptions(options) { - this.#options = { ...options }; + setOptions(options: Partial) { + this.#options = { ...this.#options, ...options } as MapOptions; MapOptionsChangedEvent.dispatch(this.#options); } @@ -1071,7 +1071,7 @@ export class Map extends L.Map { false, undefined, undefined, - (hash) => { + (hash: string) => { this.addTemporaryMarker( e.latlng, this.#spawnRequestTable?.unit.unitType ?? "unknown", @@ -1239,7 +1239,7 @@ export class Map extends L.Map { this.#lastMouseCoordinates = e.latlng; MouseMovedEvent.dispatch(e.latlng); - getGroundElevation(e.latlng, (elevation) => { + getGroundElevation(e.latlng, (elevation: number) => { MouseMovedEvent.dispatch(e.latlng, elevation); }); @@ -1366,8 +1366,8 @@ export class Map extends L.Map { .filter((unit) => !unit.getHuman()); Object.keys(this.#destinationPreviewMarkers).forEach((ID) => { - this.#destinationPreviewMarkers[ID].removeFrom(this); - delete this.#destinationPreviewMarkers[ID]; + this.#destinationPreviewMarkers[parseInt(ID)].removeFrom(this); + delete this.#destinationPreviewMarkers[parseInt(ID)]; }); if (this.#keepRelativePositions) { @@ -1385,7 +1385,7 @@ export class Map extends L.Map { #moveDestinationPreviewMarkers() { if (this.#keepRelativePositions) { Object.entries(getApp().getUnitsManager().computeGroupDestination(this.#destinationRotationCenter, this.#destinationRotation)).forEach(([ID, latlng]) => { - this.#destinationPreviewMarkers[ID]?.setLatLng(latlng); + this.#destinationPreviewMarkers[parseInt(ID)]?.setLatLng(latlng); }); } else { Object.values(this.#destinationPreviewMarkers).forEach((marker) => {