S
S
Svyatoslav Khusamov2017-04-21 22:20:53
typescript
Svyatoslav Khusamov, 2017-04-21 22:20:53

How to correctly create a method overload?

An external library type declaration is given. I quote the used parts from it:

interface IRouterMatcher<T> {
    (path: PathParams, ...handlers: RequestHandler[]): T;
    (path: PathParams, ...handlers: RequestHandlerParams[]): T;
}

interface IRouterHandler<T> {
    (...handlers: RequestHandler[]): T;
    (...handlers: RequestHandlerParams[]): T;
}


interface IRouter extends RequestHandler {
    // ...
    use: IRouterHandler<this> & IRouterMatcher<this>;
    // ...
}

Based on this declaration, I need to create a class with a use() method. It is clearly visible here that you need to create a method overload. I tried to do it like this:
export default class Router extends RequestHandler {
    // ...
    use(path: PathParams, ...handlers: RequestHandler[]): this
    use(path: PathParams, ...handlers: RequestHandlerParams[]): this
    use(...handlers: RequestHandler[]): this
    use(...handlers: RequestHandlerParams[]): this {
        // ...
        return this;
    }
    // ...
}

Which got this error:
error TS2394: Overload signature is not compatible with function implementation.

How do I properly create a use() method?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question