`Phaser.Structs.Map` has unsatisfactory and incorrect typing
Author: Bertie690Created Jun 24, 2026Updated Jul 10, 2026
Version
- Phaser Version: 4.21.0
- Operating system: N/A
- Browser: N/A
Description
This is a conglomerate of typing issues related to the Map struct:
- The type of
keyshould be constrained tostring | number- attempting to index the map with anything else will result in potentially unsafe string coercion. (I have yet to see an example where such coercion is even remotely useful.) setAllshould not have extra generic type parameters - the separateK, Vtypes used shadow the main Map's types and allow assigning completely incompatible and unexpected values. (It doesn't even enforce that the values being passed are themselves arrays or array-like objects, despite that very function crashing if said invariant is violated.)- Even though
jsdocdoesn't support emitting tuples in declarations (rendering the "correct" type of[K, V][]unusable for now), a nested array type like(K|V)[][]would still be better than one which effectively disables type safety altogether.
- Even though
getdoesn't includeundefinedin its type signature, despite mentioning as such in its JSDoc comment. This is ripe territory for game crashes andundefinedproperty accesses.- The JSDoc claims that the class constructor's
elementsparameter is optional, but the constructor states it as required to be passed.
Example Test Code
Indexing maps with an object leads to undesireable behaviour, despite being A-OK as far as types are concerned
import Phaser from "phaser";
// Error - constructor needs empty array despite being "optional"
const myMap = new Phaser.Structs.Map<string, number>();
// should produce type error but doesn't, despite these not even being tuples
myMap.setAll([() => 1, "apples"]);
const elem = myMap.get("11");
// Oops! Elem could be undefined and you might get a game crash!
console.log(elem.toFixed(2));
Additional Information
Source: phaserjs/phaser