Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Typing Proposal - Overload find Service Method #1970

Closed
robbyphillips opened this issue May 27, 2020 · 1 comment
Closed

Typing Proposal - Overload find Service Method #1970

robbyphillips opened this issue May 27, 2020 · 1 comment

Comments

@robbyphillips
Copy link
Contributor

robbyphillips commented May 27, 2020

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.

@GVanderLugt
Copy link

GVanderLugt commented May 27, 2020

I'm new to TypeScript, so I'm not comfortable commenting on the implementation, but this would definitely be nice to have.

+1

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants