Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript doesn't select the correct overload

  • typescript version: 2.1.4
  • @types/mongoose version: 4.7.1

I use mongoose's types:

post<T extends Document>(method: string, fn: (
  error: mongodb.MongoError, doc: T, next: (err?: NativeError) => void
) => void): this;

post<T extends Document>(method: string, fn: (
  doc: T, next: (err?: NativeError) => void
) => void): this;

in my code:

function (schema: Schema) {
  schema.post('remove', function (doc, next) { });
}

It always choose first post define, doc will be mongodb.MongoError, next will be T.

Did I miss something? and how to make it choose second post define?

like image 281
7Hd Avatar asked Sep 06 '25 22:09

7Hd


1 Answers

TypeScript always selects the first overload that matches the specified parameters. Because function arguments may be ignored by the callee, this means that functions accepting callbacks of higher arity should come after callbacks of lower arity.

The second overload is more specific (it can handle a function of lower total arity than the previous overload), so the second overload should be listed above the first one in this case.

TL;DR: .d.ts file has a bug, those two post lines should be swapped.

like image 156
Ryan Cavanaugh Avatar answered Sep 09 '25 22:09

Ryan Cavanaugh