Skip to content

useRxData doesn't support findOne #45

@nirvdrum

Description

@nirvdrum

I've run into a type definition error when providing useRxData with a query constructor that uses findOne. I think the issue is that findOne can return null, while find always returns a value, even if that value is an empty array. The TypeScript error isn't the clearest thing in the world:

TS2769: No overload matches this call.
  Overload 1 of 3, '(collectionName: string, queryConstructor: QueryConstructor<TodoChainDoc> | undefined): RxQueryResultDoc<TodoChainDoc>', gave the following error.
    Argument of type '(collection: RxCollection<TodoChainDoc>) => RxQuery<TodoChainDoc, RxDocument<TodoChainDoc, {}> | null>' is not assignable to parameter of type 'QueryConstructor<TodoChainDoc>'.
      Type 'RxQuery<TodoChainDoc, RxDocument<TodoChainDoc, {}> | null>' is not assignable to type 'RxQuery<TodoChainDoc, RxDocument<TodoChainDoc, {}>> | RxQuery<TodoChainDoc, RxDocument<TodoChainDoc, {}>[]> | undefined'.
        Type 'RxQuery<TodoChainDoc, RxDocument<TodoChainDoc, {}> | null>' is not assignable to type 'RxQuery<TodoChainDoc, RxDocument<TodoChainDoc, {}>>'.
          The types of '$.value' are incompatible between these types.
            Type 'RxDocument<TodoChainDoc, {}> | null' is not assignable to type 'RxDocument<TodoChainDoc, {}>'.
              Type 'null' is not assignable to type 'RxDocument<TodoChainDoc, {}>'.
  Overload 2 of 3, '(collectionName: string, queryConstructor: QueryConstructor<TodoChainDoc> | undefined, options?: Override<UseRxQueryOptions, { ...; }> | undefined): RxQueryResultDoc<...>', gave the following error.
    Argument of type '(collection: RxCollection<TodoChainDoc>) => RxQuery<TodoChainDoc, RxDocument<TodoChainDoc, {}> | null>' is not assignable to parameter of type 'QueryConstructor<TodoChainDoc>'.
  Overload 3 of 3, '(collectionName: string, queryConstructor: QueryConstructor<TodoChainDoc> | undefined, options?: Override<UseRxQueryOptions, { ...; }> | undefined): RxQueryResultJSON<...>', gave the following error.
    Argument of type '(collection: RxCollection<TodoChainDoc>) => RxQuery<TodoChainDoc, RxDocument<TodoChainDoc, {}> | null>' is not assignable to parameter of type 'QueryConstructor<TodoChainDoc>'.

I can manually query against the collection or just use find and unwrap the array in the meanwhile. Please let me know if I can be of any assistance. I couldn't see a simple way to change the return type based on using find vs findOne and I don't want to break code on existing users by forcing null as a potential return value on all queries. Maybe the solution is a new hook (e.g, useRxDataOne)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions