Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Signature Mismatch in MiddlewareInterface #21

Closed
JD-Robbs opened this issue Aug 17, 2016 · 6 comments
Closed

Signature Mismatch in MiddlewareInterface #21

JD-Robbs opened this issue Aug 17, 2016 · 6 comments

Comments

@JD-Robbs
Copy link
Contributor

Consider the following code (note the Request/Response type hints):

export class MyMiddleware implements MiddlewareInterface {
    use(request: Request, response: Response, next?: Function): any {

Compiling throws the following error:

error TS2420: Class 'MyMiddleware' incorrectly implements interface 'MiddlewareInterface'.
  Types of property 'use' are incompatible.
    Type '(request: Request, response: Response, next?: Function) => any' is not assignable to type '{ (request: any, response: any, next?: (err?: any) => any): any; (context: any, next: (err?: any)...'.
      Types of parameters 'response' and 'next' are incompatible.
        Type '(err?: any) => Promise<any>' is not assignable to type 'Response'.
          Property 'status' is missing in type '(err?: any) => Promise<any>'.

This, however, works:

export class MyMiddleware implements MiddlewareInterface {
    use(request: any, response: any, next?: Function): any {

I tracked the issue to the second (Koa) signature in MiddlewareInterface (removing it fixes the above but, obviously, won't make Koa happy). It looks like my code makes the TypeScript compiler think I'm trying to implement the second signature.

While I tried changing/adding signatures in MiddlewareInterface, I couldn't get it to compile.

@pleerock
Copy link
Contributor

Not sure what is possible there. I know about this issue with typescript, one solution can be if we create two separate interfaces KoaMiddlewareInterface and ExpressMiddlewareInterface instead of MiddlewareInterface. If you can come to solution with MiddlewareInterface please let me know

@JD-Robbs
Copy link
Contributor Author

If I can work out a way to keep it all in one interface, I'll let you know. I'll take another look and maybe check if anyone on StackOverlow has any ideas.

@marshall007
Copy link

marshall007 commented Apr 14, 2017

@JD-Robbs I was able to get it to compile by overloading the method on my custom class with a more generic signature:

@Middleware()
export class CustomerHeader implements MiddlewareInterface {

  use(request: any, response: any, next?: (err?: any) => any): any;
  use(request: Request, response: Response, next?: NextFunction): any {
    // ...
    next();
  }

}

@pleerock I think if you annotate MiddlewareInterface with the additional overloads it should allow users to implement the method with or without typed parameters.

@pleerock
Copy link
Contributor

new middleware interface signatures are being added in #119

@pleerock pleerock mentioned this issue Apr 24, 2017
@pleerock
Copy link
Contributor

middlewares now have separate signatures in 0.7.0

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants