Skip to content

Make LoroMap.entries() generic to support branded string types #889

@danielo515

Description

@danielo515

Problem

The entries() method on LoroMap currently has a fixed return type:

entries(): ([string, Value | Container])[];

This makes it difficult to use with branded/nominal string types for keys. Many TypeScript codebases use branded types for IDs to provide additional type-level safety:

type SeminarId = string & { readonly _brand: unique symbol };
type RoomId = string & { readonly _brand: unique symbol };

// When iterating over a map with SeminarId keys, we lose the brand information
const map: LoroMap<SeminarId, SomeValue> = ...;
const entries = map.entries(); // Returns [string, Value | Container][] - SeminarId brand is lost

Proposed Solution

Make the entries() method generic to preserve the key type:

entries(): ([K, Value | Container])[];
// or if LoroMap already has a key type parameter:
entries(): ([KeyType, Value | Container])[];

This would allow branded string types to flow through correctly:

const entries = map.entries(); // Returns [SeminarId, Value | Container][]

Use Case

We're building a planning system where we have multiple entity types (seminars, rooms, bookings, etc.) each with their own branded ID type. When iterating over CRDT maps, we currently lose this type information and have to cast manually, which defeats the purpose of having branded types for type safety.

Environment

  • loro-crdt version: 1.8.8
  • TypeScript 5.9

Thank you for considering this enhancement!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions