@mocanvas/store · function

assertIdType

declare function assertIdType<R extends UnknownRecord>(id: string | undefined, type: RecordType<R, any>): asserts id is IdOf<R>

Assert that id belongs to type, narrowing it to that type's id.

Record ids are branded strings, so the compiler already stops most mix-ups — but ids that arrive from outside the program (a URL, a saved file, a sync message) are plain strings that someone has to vouch for. This is the place to do that vouching: it throws with the offending id rather than letting a page: id be looked up as a shape and quietly returning undefined.

``ts assertIdType(idFromUrl, PageRecordType) editor.setCurrentPage(idFromUrl) // now typed as TLPageId ``

mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.