Skip to content

TypeScript: document ids can be strings or numbers - #662

Merged
pubkey merged 2 commits into
masterfrom
ccr-94faca52-vf6ypt-ts-id-types
Nov 13, 2026
Merged

pubkey merged 2 commits into
masterfrom
ccr-94faca52-vf6ypt-ts-id-types

Conversation

@pubkey

@pubkey pubkey commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Problem

Document ids do not have to be strings: number ids work at runtime, and ids are compared with === without conversion (#650). But the TypeScript types only allowed string:

  • ChangeEvent.id: string
  • QueryParams.getDocumentId: (doc) => string
  • ResultKeyDocumentMap = Map<string, DocType>

So TypeScript users with number ids had to cast with as any. The existing id test did that too.

Fix

  • New exported type DocumentId = string | number.
  • It is used for ChangeEvent.id, the return value of getDocumentId, the keys of ResultKeyDocumentMap, and the util functions getDocumentId() and indexOfDocumentId().
  • The truth-table generator's Collection.remove() takes a DocumentId.
  • The README sample says that ids can be strings or numbers and that the id of a change event must have the same type as the id of the document.

No runtime code is changed.

Note for users: code that reads changeEvent.id or a key of the key document map into a string variable no longer compiles and needs a DocumentId type or a narrowing check. Code that only passes values in is not affected.

Tests

id-types.test.ts no longer casts with as any, and has a new fully typed test with number ids. Without the type change it does not compile (5 tsc errors in that file). Full suite: 368 passing, eslint and tsc clean.


Generated by Claude Code

Number ids work at runtime, but the types only allowed strings.
Adds the DocumentId type (string | number) and uses it for the id of a
change event, the return value of getDocumentId and the keys of the
key document map.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6jP7VRViW2owS6ziiF
The example did not build with the widened id type: it stored
changeEvent.id in maps and arrays typed as string.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015P6w6jP7VRViW2owS6ziiF
@pubkey
pubkey merged commit b46ddbb into master Nov 13, 2026
8 checks passed
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