8000 Typing Proposal - Overload `find` Service Method · Issue #1970 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content

Typing Proposal - Overload find Service Method #1970

@robbyphillips

Description

@robbyphillips

Expected behavior

It would be nice if the return type was automatically inferred from adapter services based on params.paginate

let users: User[] = await app.service('users').find({ paginate: false })
let paginated: Paginated<User> = await app.service('users').find()

Actual behavior

Instead, the type system does not know which return type to use, so we need to cast it.

let paginated = await app.service('users').find() as Paginated<User>

Proposal

I think we can solve this with an overload. Something like:

interface ParamsNoPaginate extends Params {  // not a great name, I know..
  paginate: false
}

// service.d.ts
export declare class AdapterService<T = any> implements ServiceMethods<T> {
  // ...
  find(params: ParamsNoPaginate): Promise<T[]>
  find(params?: Params): Promise<Paginate<T>>
  // ...
}

I'm less comfortable with conditional types, but I suppose that would work as well and it would avoid having to extend Params.

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

      0