为什么打字稿会忽略RxJS过滤器

时间:2017-04-19 05:16:59

标签: angular filter event-handling rxjs

我试图过滤角度事件,但打字稿并不喜欢它。使用此代码:

this.router.events
.filter(e => e instanceof RoutesRecognized)
  .subscribe(data => {
    console.log(data.state.root.firstChild.data[0])
  });

工作并输出我在路由配置中输入的所有静态数据,例如:

{ path: 'about', component: AboutContainer, data: [{ Foo: 'bar' }] }

除了typescript抛出错误消息:

error TS2339: Property 'state' does not exist on type 'Event'. 
Property 'state' does not exist on type 'NavigationStart'.

所以它工作正常,但错误信息很烦人。我可以在if data instanceof RoutesRecognized中包装console.log,但看起来很脏。有谁知道如何使用RxJS来阻止错误?

1 个答案:

答案 0 :(得分:0)

@echonax建议.subscribe((data: any) => {效果很好。我最终投射到RoutesRecognized更具体。这删除了错误消息:

.subscribe( (data: RoutesRecognized) => {