TanStackT
TanStack4mo ago
10 replies
rubber-blue

type narrowing in live queries

Hey all! I've got a series of events which I'd like to filter out (and type narrow) in order to produce aggregates. Here's what I have so far:

export const eventCollection = createCollection(
  localOnlyCollectionOptions({
    id: 'all-events',
    schema: EventSchema,
    getKey: (event) => event.timestamp,
  }),
)

export const pingEventCollection = createLiveQueryCollection({
  id: 'ping-events',
  query: (q) =>
    q.from({ event: eventCollection }).where(({ event }) => eq(event.type, 'PingAdded')),
})


The problem I have here is that pingEventCollection ends up with the full EventSchema type instead of being properly narrowed to just PingAddedV1Schema. How can I get this type narrowing?
Was this page helpful?