Repository navigation
TypeScript: document ids can be strings or numbers - #662
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 allowedstring:ChangeEvent.id: stringQueryParams.getDocumentId: (doc) => stringResultKeyDocumentMap = Map<string, DocType>So TypeScript users with number ids had to cast with
as any. The existing id test did that too.Fix
DocumentId = string | number.ChangeEvent.id, the return value ofgetDocumentId, the keys ofResultKeyDocumentMap, and the util functionsgetDocumentId()andindexOfDocumentId().Collection.remove()takes aDocumentId.No runtime code is changed.
Note for users: code that reads
changeEvent.idor a key of the key document map into astringvariable no longer compiles and needs aDocumentIdtype or a narrowing check. Code that only passes values in is not affected.Tests
id-types.test.tsno longer casts withas 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