简体   繁体   English

如何解决错误? 打字稿:类型'ErrorRequestHandler'不能分配给类型'IMiddleware'

[英]How do I fix the error? Typescript: Type 'ErrorRequestHandler' is not assignable to type 'IMiddleware'

In the following code, I got an error with type reassignment: 在以下代码中,我遇到了类型重新分配错误:

export default class AbstractController extends Middleware {

    get middleware(): Express.RequestHandler | Express.ErrorRequestHandler { // (6,22)
        //...
    }

}

Error text: 错误文字:

src/controller/AbstractController.ts(6,22): error TS2415: Class 'AbstractController' incorrectly extends base class 'Middleware'.
  Types of property 'middleware' are incompatible.
    Type 'ErrorRequestHandler | RequestHandler' is not assignable to type 'IMiddleware'.
      Type 'ErrorRequestHandler' is not assignable to type 'IMiddleware'.

Declare RequestHandler and ErrorRequestHandler interfaces: 声明RequestHandler和ErrorRequestHandler接口:

interface RequestHandler {
    (req: Request, res: Response, next: NextFunction): any;
}

interface ErrorRequestHandler {
    (err: any, req: Request, res: Response, next: NextFunction): any;
}

Declare Middleware class: 声明中间件类:

export interface IMiddleware {
    (req: http.IncomingMessage, res: http.ServerResponse): void;
}

export default class Middleware {

    get middleware(): IMiddleware {
        throw new Error('Не определен middleware.');
    }

}

I need to understand not how to fix it, but what is the cause of the error. 我不需要了解如何解决它,而是要了解导致错误的原因。

The Middleware class expects a IMiddleware as return type of middleware() . Middleware类期望将IMiddleware作为middleware()返回类型。

Your interfaces should extend IMiddleware ! 您的界面应扩展IMiddleware!

interface RequestHandler extends IMiddleware {
    (req: Request, res: Response, next: NextFunction): any;
}

interface ErrorRequestHandler extends IMiddleware {
    (err: any, req: Request, res: Response, next: NextFunction): any;
}

However, ErrorRequestHandler 's signature does not match IMiddleware 但是, ErrorRequestHandler的签名与IMiddleware不匹配

Maybe you should create a second interface to represent it. 也许您应该创建第二个接口来表示它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复错误类型 void is not assignable to type using react and typescript? - How to fix error type void is not assignable to type using react and typescript? 如何解决打字稿中无法将X类型分配给Y类型错误的问题? - How can I fix this type X is not assignable to type Y error in typescript? 打字稿修复错误类型的参数不可分配 - typescript fix error argument of type is not assignable VSCode 如何修复 typescript 突出显示错误 {} 类型的参数不可分配给 - VSCode how to fix typescript highlight error Argument of type {} is not assignable to parameter of TypeScript 错误:类型 &#39;type&#39; 不可分配给类型 &#39;IntrinsicAttributes &amp; &#39;type&#39; &amp; { children?: ReactNode; }&#39;。 如何解决? - TypeScript Error: Type 'type' is not assignable to type 'IntrinsicAttributes & 'type' & { children?: ReactNode; }'. How to fix it? 打字稿“类型不可分配给类型”错误 - Typescript "Type is not assignable to type" error Typescript 错误:键入此不能分配给键入此 - Typescript error: type this is not assignable to type this 如何修复 typescript 错误类型“string”不可分配给类型“Ref”<inputref> | 不明确的'?</inputref> - How to fix typescript error Type 'string' is not assignable to type 'Ref<InputRef> | undefined'? 打字稿:不可分配给键入错误 - Typescript: is not assignable to type error 如何在Typescript中修复此类型推断 - How do I fix this type inference in Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM