Skip to content

queryOnce should preserve SingleResult type from source collection #1397

@rohanajayjain

Description

@rohanajayjain

When a collection is created with .findOne(), it's typed as Collection<T> & SingleResult, correctly indicating it returns at most one row. However, when this collection is passed to queryOnce, the SingleResult information is lost:

// Collection correctly typed as SingleResult
const collection = createLiveQueryCollection({
  query: (q) => q.from({ users }).where(...).findOne(),
}); // Collection<User> & SingleResult

// queryOnce wraps it in q.from(), losing SingleResult - returns User[] instead of User | undefined

const result = await queryOnce((q) => q.from({ user: collection }));
// ^? User[]  — have to destructure: const [user] = result

Expected behavior:

queryOnce should either:

  1. Accept a SingleResult collection directly and return Promise<T | undefined>, or
  2. Preserve the SingleResult marker through q.from() so InferResultType returns T | undefined instead of T[]

Current workaround:

const [result] = await queryOnce((q) =>
  q.from({ config: singleResultCollection }),
);

This works but loses type safety - result is typed as T | undefined only because of array destructuring, not because the type system knows it's a single-result query.

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