Skip to content

Conversation

Copy link

Copilot AI commented Jan 31, 2026

.findOne() and cursor methods were returning Partial<T> unconditionally, forcing consumers to handle potentially undefined fields even when no projection was used:

interface User {
  name: string;
  email: string;
  age: number;
}

const user = await collection.findOne({ name: 'Alice' });
// Previously: user.name was string | undefined
// Now: user.name is string

Changes

  • Updated return types:

    • findOne(): Promise<Partial<T> | null>Promise<T | null>
    • FindCursor.toArray(): Promise<Partial<T>[]>Promise<T[]>
    • FindCursor.first(): Promise<Partial<T> | null>Promise<T | null>
  • Added JSDoc remarks explaining that projected fields may be undefined at runtime despite the type being T, accepting this trade-off for better ergonomics in the common case

Behavior with projections

When projections are used, fields are undefined at runtime but typed as T:

const user = await collection.findOne({ name: 'Alice' }, { name: 1 });
// user.email is typed as string but undefined at runtime

This matches MongoDB driver behavior and prioritizes the common non-projection case.

Original prompt

This section details on the original issue you should resolve

<issue_title>.findOne returns partial?</issue_title>
<issue_description>Hi, I'm considering using this module for a local project. Is there a specific reason .findOne returns a partial of the provided type?

async findOne(filter: Filter<T>, projection?: Projection<T>): Promise<Partial<T> | null> {

Should it not return the full type? (null | T instead of null | Partial)
Also, when providing types to MongoLiteCollection, it seems a bit counter intuitive that the document has to satisfy DocumentWithId (which has an open signature of [key: string]: unknown) when we have a known, specific type.

Thank you for your time! Very useful project, love the mongodb-like API</issue_description>

<agent_instructions>Can you help resolve the issue raised by this user, I would agree, we should return the full type where possible</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits January 31, 2026 19:17
…Partial<T>

Co-authored-by: semics-tech <47321195+semics-tech@users.noreply.github.com>
Co-authored-by: semics-tech <47321195+semics-tech@users.noreply.github.com>
Co-authored-by: semics-tech <47321195+semics-tech@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix .findOne to return full type instead of partial Return full type T from findOne() and cursor methods instead of Partial<T> Jan 31, 2026
Copilot AI requested a review from semics-tech January 31, 2026 19:20
@semics-tech
Copy link
Owner

@NewYanko please let me know if this change addresses your comment on #15, I'm no typescript expert so would appreciate your input!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.findOne returns partial?

2 participants