Interface ReadonlyMap<K, V>

A Map object which cannot be written to. The Map object holds key-value pairs but doesn't remember the original insertion order of the keys (like JS would). Any value (both objects and primitive values) may be used as either a key or a value. Maps are the best choice for dynamic indexing/newindexing, whereas Objects are better for explicit indexing.

Example

// ReadonlyMaps are particularly useful for defining readonly-associations with non-numeric, non-string keys.
new ReadonlyMap<Enum.HumanoidRigType, () => void>([
[Enum.HumanoidRigType.R6, () => {}],
[Enum.HumanoidRigType.R15, () => {}],
]);
// Do not use Maps when you can easily index from an object:

// TS doesn't assume "x" | "y" are the only possible fields.
// You could manually type this as ReadonlyMap<"x" | "y", number>
const point = new ReadonlyMap([["x", 5], ["y", 10]]);
// this is typed as possibly undefined, which isn't ideal
print(point.get("x"));

// Instead use an object
const point = { x: 5, y: 10 } as const;
print(point.x);
interface ReadonlyMap<K, V> {
    forEach(this, callbackfn): void;
    get(this, key): undefined | V;
    has(this, key): boolean;
    isEmpty(this): boolean;
    size(this): number;
}

Type Parameters

  • K
  • V

Hierarchy (view full)

  • Iterable<[K, V]>

Methods

  • Performs the specified action for each (element / pair of elements) in the Map

    Parameters

    • this: ReadonlyMap<K, V>
    • callbackfn: ((value, key, self) => void)

      A function that accepts up to three arguments. forEach calls the callbackfn function one time for each (element / pair of elements) in the array.

        • (value, key, self): void
        • Parameters

          • value: V
          • key: K
          • self: this

          Returns void

    Returns void

  • Returns the value associated with the given key

    Parameters

    Returns undefined | V

  • Returns a boolean for whether the given key exists in the Map

    Parameters

    Returns boolean

  • Returns true if empty, otherwise false.

    Parameters

    Returns boolean

  • Returns the number of elements in the Map

    Parameters

    Returns number