简体   繁体   English

Angular 错误 TS2345:“MonoTypeOperatorFunction”类型的参数<event> ' 不可分配给 'OperatorFunction 类型的参数<event, event> '</event,></event>

[英]Angular Error TS2345: Argument of type 'MonoTypeOperatorFunction<Event>' is not assignable to parameter of type 'OperatorFunction<Event, Event>'

Trying to troubleshoot this code,试图解决此代码,

import { Event, Router, RoutesRecognized, ActivationStart, NavigationEnd } from "@angular/router";

...

this._router.events.pipe(
    filter((evt: Event) => evt instanceof RoutesRecognized),
    map((evt: RoutesRecognized) => evt.state.root),
).subscribe((rootRoute: ActivatedRouteSnapshot) => (
    this._resolvedRoutes = this._getResolvedRoutes(rootRoute.children)
));

Which is producing this error,哪个产生这个错误,

ERROR: projects/acme/angular-ui/src/lib/services/breadcrumb/breadcrumb.service.ts:213:11 - error TS2345: Argument of type 'MonoTypeOperatorFunction<Event>' is not assignable to parameter of type 'OperatorFunction<Event, Event>'.
  Types of parameters 'source' and 'source' are incompatible.

What exactly is the problem here?这里的问题到底是什么? I'm guessing the MonoTypeOperatorFunction<Event> function is the filter, but I have no idea what the OperatorFunction<Event, Event> why would there be two type-variables in the map?我猜MonoTypeOperatorFunction<Event> function 是过滤器,但我不知道OperatorFunction<Event, Event>为什么 map 中会有两个类型变量? How do I resolve this?我该如何解决这个问题?

Use a user-defined type-guard使用用户定义的类型保护

I tried this previously, but this is the format you'll want:我以前试过这个,但这是你想要的格式:

this._router.events.pipe(
    filter((evt: Event): evt is RoutesRecognized => evt instanceof RoutesRecognized),
    map((evt: RoutesRecognized) => evt.state.root),
).subscribe((rootRoute: ActivatedRouteSnapshot) => (
    this._resolvedRoutes = this._getResolvedRoutes(rootRoute.children)
));

Now in my case, that produces this error:现在就我而言,这会产生此错误:

ERROR: projects/acmeco/angular-ui/src/lib/services/breadcrumb/breadcrumb.service.ts:213:11 - error TS2345: Argument of type 'import("/home/ecarroll/code/js/myworkspace/projects/acmeco/angular-ui/node_modules/rxjs/internal/types").OperatorFunction<import("/home/ecarroll/code/js/myworkspace/node_modules/@angular/router/router").Event, import("/home/ecarroll/code/js/myworkspace/node_modules/@angular/router/router").RoutesRecognized>' is not assignable to parameter of type 'import("/home/ecarroll/code/js/myworkspace/node_modules/rxjs/internal/types").OperatorFunction<import("/home/ecarroll/code/js/myworkspace/node_modules/@angular/router/router").Event, import("/home/ecarroll/code/js/myworkspace/node_modules/@angular/router/router").RoutesRecognized>'.
  Types of parameters 'source' and 'source' are incompatible.
    Type 'import("/home/ecarroll/code/js/myworkspace/node_modules/rxjs/internal/Observable").Observable<import("/home/ecarroll/code/js/myworkspace/node_modules/@angular/router/router").Event>' is not assignable to type 'import("/home/ecarroll/code/js/myworkspace/projects/acmeco/angular-ui/node_modules/rxjs/internal/Observable").Observable<import("/home/ecarroll/code/js/myworkspace/node_modules/@angular/router/router").Event>'.
      The types of 'source.operator.call' are incompatible between these types.
        Type '(subscriber: import("/home/ecarroll/code/js/myworkspace/node_modules/rxjs/internal/Subscriber").Subscriber<any>, source: any) => import("/home/ecarroll/code/js/myworkspace/node_modules/rxjs/internal/types").TeardownLogic' is not assignable to type '(subscriber: import("/home/ecarroll/code/js/myworkspace/projects/acmeco/angular-ui/node_modules/rxjs/internal/Subscriber").Subscriber<any>, source: any) => import("/home/ecarroll/code/js/myworkspace/projects/acmeco/angular-ui/node_modules/rxjs/internal/types").TeardownLogic'.
          Types of parameters 'subscriber' and 'subscriber' are incompatible.
            Type 'import("/home/ecarroll/code/js/myworkspace/projects/acmeco/angular-ui/node_modules/rxjs/internal/Subscriber").Subscriber<any>' is not assignable to type 'import("/home/ecarroll/code/js/myworkspace/node_modules/rxjs/internal/Subscriber").Subscriber<any>'.
              Property 'isStopped' is protected but type 'Subscriber<T>' is not a class derived from 'Subscriber<T>'.

This error is because TypeScript is unsound, and you have two similarly named types.此错误是因为 TypeScript 不正确,并且您有两个类似命名的类型。 You can get more information about the conflict with npm list rxjs .您可以通过npm list rxjs获得有关冲突的更多信息。 You can fix this problem following this blog post ,您可以按照这篇博文解决此问题,

By adding to your tsconfig.json通过添加到您的tsconfig.json

"paths": {
    "rxjs": [
      "node_modules/rxjs"
    ],
    "rxjs/*": [
      "node_modules/rxjs/*"
    ]
}

This will force all of your versions to use the same version of rxjs .这将强制您的所有版本使用相同版本的rxjs

I had similar situation while writing an interceptor.我在编写拦截器时遇到了类似的情况。

export class MyInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    return next.handle(req).pipe(
      filter(event => event instanceof HttpResponse),
      map((event: HttpResponse<any>) => {
        // clone event and do stuff
        return event;
      })
    );
  }
}

On 'filter' I got the following error:在“过滤器”上,我收到以下错误:

Type 'Observable<HttpEvent>' is not assignable to type 'Observable<HttpResponse>'.类型“Observable<HttpEvent>”不可分配给类型“Observable<HttpResponse>”。

The solution with "paths" in tsconfig.json didn't help, so I just worked it around by writing it the following way: tsconfig.json 中带有“路径”的解决方案没有帮助,所以我只是通过以下方式编写来解决它:

export class MyInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    return next.handle(req).pipe(
      map(event => {
        if (event instanceof HttpResponse) {
          // clone event and do stuff
        }
        return event;
      })
    );
  }
}

Works like a charm.奇迹般有效。 :) :)

暂无
暂无

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

相关问题 TS2345:“事件”类型的参数不可分配给“HtmlInputEvent”类型的参数 - TS2345: Argument of type 'Event' is not assignable to parameter of type 'HtmlInputEvent' 错误 TS2345:类型为“OperatorFunction”的参数 <object, object> ' 不能分配给 'OperatorFunction 类型的参数 </object,> - error TS2345: Argument of type 'OperatorFunction<Object, Object>' is not assignable to parameter of type 'OperatorFunction 错误 TS2345:类型为“OperatorFunction”的参数<user, void> ' 不能分配给 'OperatorFunction 类型的参数<object, void> '</object,></user,> - error TS2345: Argument of type 'OperatorFunction<User, void>' is not assignable to parameter of type 'OperatorFunction<Object, void>' 错误 TS2345:“事件”类型的参数不可分配给“IPokemon”类型的参数 - error TS2345: Argument of type 'Event' is not assignable to parameter of type 'IPokemon' 错误 TS2345:“事件”类型的参数不可分配给“字符串”类型的参数 - error TS2345: Argument of type 'Event' is not assignable to parameter of type 'string' 错误 TS2345:“事件”类型的参数不可分配给“{目标:{值:字符串; }; }' - error TS2345: Argument of type 'Event' is not assignable to parameter of type '{ target: { value: string; }; }' 错误TS2345:“菜单”类型的参数无法分配给类型参数 - error TS2345: Argument of type 'Menu' is not assignable to parameter of type 错误TS2345:“对象”类型的参数无法分配给类型的参数 - Error TS2345: Argument of type 'Object' is not assignable to parameter of type Angular 5错误TS2345:“数字”类型的参数无法分配给“字符串”类型的参数 - Angular 5 error TS2345: Argument of type 'number' is not assignable to parameter of type 'string' Angular2 / TypeScript:错误TS2345:类型'String'的参数不能分配给'string'类型的参数 - Angular2/TypeScript: error TS2345: Argument of type 'String' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM