Skip to content

Snapshots of MapSchema ought to be partial - #14

Merged
endel merged 2 commits into
mainfrom
snapshot-type
Oct 3, 2026
Merged

endel merged 2 commits into
mainfrom
snapshot-type

Conversation

@FTWinston

Copy link
Copy Markdown
Collaborator

A snapshot of a MapSchema<T> (or of a IMap<string, T>) is typed as Record<string, T> which is awkward for testing it e.g. in storybook. If it instead is typed Partial<Record<string, T>>.

A Record asserts that every key is present, whereas a Map does nothing of the sort, so I think making the frontend type Partial is the more accurate approach.

@FTWinston FTWinston changed the title MapSchema snapshots are now partial Snapshots of MapSchema ought to be partial Oct 3, 2026
Partial on every map typed Object.values() entries as possibly undefined,
and MapSchema snapshots skipped the IMap branch entirely. Wide keys keep
the index signature; only literal unions, where Record demanded every
key, become optional — for MapSchema<V, K> and IMap alike.

Assisted-by: Claude Opus 5.5
@endel
endel merged commit ff10b72 into main Oct 3, 2026
1 check passed
@endel

endel commented Oct 3, 2026

Copy link
Copy Markdown
Member

Thanks @FTWinston! Merged and released in 0.18.3.

I adjusted the approach before merging:

  • Union keys become optional, for both IMap<K, V> and MapSchema<V, K>. MapSchema is matched by an earlier branch of Snapshot, so the original change never reached it (that branch also dropped K). Snapshot of IMap<CardTrait, ExtraTraitType> is now { readonly [P in CardTrait]?: ExtraTraitType }.
  • String keys keep their index signature. Partial<Record<string, T>> resolves to { [k: string]: T | undefined }, which turns every Object.values(snap.players).map(p => p.name) into a 'p' is possibly 'undefined' error, though entries are never undefined at runtime. To get checked lookups on string-keyed maps, noUncheckedIndexedAccess types snap[id] as T | undefined and keeps iteration exact.

I checked this against AllHands: on 0.18.3 the as Record<CardTrait, ExtraTraitType> cast in WeaponSlot.stories.tsx can go (along with the then-unused CardTrait import), and the rest of the project type-checks unchanged.

@FTWinston
FTWinston deleted the snapshot-type branch October 3, 2026 09:04
@FTWinston

Copy link
Copy Markdown
Collaborator Author

Nice one, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants