Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/schema/createSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,23 @@ type Primitive = string | number | boolean | bigint | symbol | null | undefined;
/** Anything usable as a Map key. (MapSchema keys are always strings.) */
type MapKey = string | number | symbol;

/**
* Plain-object form of a map snapshot. Wide keys (`string`, `` `p_${string}` ``)
* stay an index signature, so `Object.values()` yields defined entries. A finite
* key union (`"red" | "blue"`) becomes optional properties, since a map need not
* hold every key. The test: an empty object only satisfies a `Record` with no
* required keys.
*/
type MapSnapshot<K extends MapKey, V> = Record<never, never> extends Record<K, unknown>
? Record<K, Snapshot<V>>
: Partial<Record<K, Snapshot<V>>>;

/**
* Transforms a Colyseus Schema type into an immutable, plain JavaScript type.
*
* - `ArraySchema<T>` (or `Array<T>` / `ReadonlyArray<T>`) becomes `readonly T[]`
* - `MapSchema<T>` (or `Map<K, T>` / `ReadonlyMap<K, T>`) becomes `Readonly<Record<K, T>>`
* - `MapSchema<T, K>` (or `Map<K, T>` / `ReadonlyMap<K, T>`) becomes `Readonly<Record<K, T>>`,
* with every key optional when `K` is a finite union (see `MapSnapshot`)
* - `Schema` subclasses (and plain objects) become plain objects with only data properties
* - Primitives remain unchanged
*
Expand All @@ -95,12 +107,12 @@ type MapKey = string | number | symbol;
export type Snapshot<T> = DeepReadonly<
T extends ArraySchema<infer U>
? Snapshot<U>[]
: T extends MapSchema<infer U>
? Record<string, Snapshot<U>>
: T extends MapSchema<infer U, infer K>
? MapSnapshot<K, U>
: T extends IArray<infer U>
? Snapshot<U>[]
: T extends IMap<infer K extends MapKey, infer U>
? Record<K, Snapshot<U>>
? MapSnapshot<K, U>
: T extends Primitive
? T
: T extends object
Expand Down
11 changes: 11 additions & 0 deletions src/tests/snapshotType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ expectMutual<Snapshot<PlayerState>["weapons"], { readonly [k: string]: W }>(true
expectMutual<Snapshot<IPlayer>["loadout"], ReadonlyArray<W>>(true);
expectMutual<Snapshot<IPlayer>["weapons"], { readonly [k: string]: W }>(true);

// Finite key unions — a map need not hold every key, so entries are optional.
type Slot = "primary" | "secondary";
type SlotW = { readonly primary?: W; readonly secondary?: W };
expectMutual<Snapshot<MapSchema<Weapon, Slot>>, SlotW>(true);
expectMutual<Snapshot<ReadonlyMap<Slot, IWeapon>>, SlotW>(true);

// Wide keys stay an index signature, so iterated entries are never `undefined`.
expectMutual<Snapshot<ReadonlyMap<`p_${string}`, IWeapon>>, { readonly [k: `p_${string}`]: W }>(true);
const values: W[] = Object.values({} as Snapshot<IPlayer>["weapons"]);
void values;

// Primitives pass through.
expectMutual<Snapshot<number>, number>(true);
expectMutual<Snapshot<string>, string>(true);
Expand Down
Loading