角度2-打字稿:TS2322:无法将“订阅"类型分配给“可观察< MouseEvent>"类型 [英] Angular 2 - Typescript: TS2322: Type 'Subscription' is not assignable to type 'Observable<MouseEvent>'

查看:225
本文介绍了角度2-打字稿:TS2322:无法将“订阅"类型分配给“可观察< MouseEvent>"类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用该插件的click-outside指令-> http://embed.plnkr. co/v7BMUv/

I am using the click-outside directive from this plunk --> http://embed.plnkr.co/v7BMUv/

我的TS编译器抛出以下错误:

My TS compiler is throwing the following errors:

TS2322:无法将预订"类型分配给可观察"类型. 订阅类型中缺少属性"_isScalar".

TS2322: Type 'Subscription' is not assignable to type 'Observable'. Property '_isScalar' is missing in type 'Subscription'.

TS2339类型可观察"的属性取消订阅"不存在.

TS2339 Property 'unsubscribe' does not exist on type 'Observable'.

我的tsconfig.json:

My tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "target": "es6",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "suppressImplicitAnyIndexErrors": true,
    "noImplicitAny": false,
    "noEmitOnError": false
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

导致错误的代码:

  ngOnInit() {
    this.globalClick = Observable
      .fromEvent(document, 'click')
      .delay(1)
      .do(() => {
        this.listening = true;
      }).subscribe((event:MouseEvent) => {
        this.onGlobalClick(event);
      });
  }
  

如何克服此错误?

推荐答案

错误在click-outside.directive.ts中. Observable.subscribe返回一个Subscription(在ngOnInit中),而不是另一个Observable.因此,private globalClick的类型应为Subscription.

The error is in click-outside.directive.ts. Observable.subscribe returns a Subscription (in ngOnInit), not another Observable. Thus, the type of private globalClick should be Subscription.

它在删除类型时起作用,并且由于plunker不会显示其类型错误,但是使用tsc进行编译时,它将在尝试将Subscription对象分配给Observable时出错.

It works when types are removed, and as plunker doesn't show type errors it worked, but when compiling with tsc it will error out as you're trying to assign a Subscription object to an Observable.

这篇关于角度2-打字稿:TS2322:无法将“订阅"类型分配给“可观察< MouseEvent>"类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆