简体   繁体   English

TSLint令人讨厌的消息

[英]TSLint annoying message

On my angular's component i'm using two methods from RxJs, debounceTime() and distinctUntilChanged() 在我的角度组件上,我正在使用RxJs中的两种方法debounceTime debounceTime()distinctUntilChanged()

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';

import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';

@Component({
  selector: 'app-form4th',
  templateUrl: './form4th.component.html',
})
export class Form4thComponent implements OnInit {
  searchField: FormControl;
  searches: string[] = [];

  constructor() { }

  ngOnInit() {
    this.searchField = new FormControl();
    this.searchField
        .valueChanges
        .debounceTime(400)
        .distinctUntilChanged()
        .subscribe(term => {
          this.searches.push(term);
        });
  }
}

App works fine , no error or even no warning message when doing (build) ie ng serve , and running the app on browser works as expected and no error message or warning too on browser console. 应用程序运行正常 ,在进行(build)即ng serve没有错误,甚至没有警告消息 ,并且在浏览器上运行该应用程序按预期工作,并且在浏览器控制台上也没有错误消息或警告。

However , I have this weird TSLint message on my vscode saying: 但是 ,我在vscode上有一条奇怪的TSLint消息,说:

[ts] Property 'debounceTime' does not exist on type 'Observable<any>'.

it's kind of annoying, since i kinda worry something doesn't work under the hood that i''m not aware of. 这有点烦人,因为我有点担心某些东西在我不了解的情况下无法正常工作。

What am i missing here? 我在这里想念什么? Thank You. 谢谢。

As explained in some comments, it's not a TSLINT error, it's a Typescript error. 如某些注释中所述,这不是TSLINT错误,而是Typescript错误。

The thing here, you're patching the prototype of Observable when you do that: import 'rxjs/add/operator/debounceTime'; import 'rxjs/add/operator/distinctUntilChanged'; 在这里,要做的是在修补Observable的原型: import 'rxjs/add/operator/debounceTime'; import 'rxjs/add/operator/distinctUntilChanged'; import 'rxjs/add/operator/debounceTime'; import 'rxjs/add/operator/distinctUntilChanged';

Instead of doing that, you might just want to take advantage of a new feature called lettable operators since rxjs v5.5. 而不是这样做,您可能只想利用自rxjs v5.5以来的新功能,称为lettable lettable operators It let you use a new .pipe operator which takes functions as argument (rxjs operators or your own). 它允许您使用一个新的.pipe运算符,该运算符将函数作为参数(rxjs运算符或您自己的)。

So instead of your code, try the following one: 因此,请尝试以下代码,而不是您的代码:

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';

// notice this import will not patch `Observable` prototype
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';

@Component({
  selector: 'app-form4th',
  templateUrl: './form4th.component.html',
})
export class Form4thComponent implements OnInit {
  searchField: FormControl;
  searches: string[] = [];

  constructor() { }

  ngOnInit() {
    this.searchField = new FormControl();

    this.searchField
      .valueChanges
      .pipe(
        debounceTime(400),
        distinctUntilChanged()
      )
      .subscribe(term => {
        this.searches.push(term);
      });
  }
}

By not patching the prototype of Observable it'll help your bundler to do tree shaking (if available) but I'm sure it'll be easier for Typescript to make the necessary checks as the functions will have to be imported in the same file. 通过不修补Observable的原型,它可以帮助捆绑程序进行树状摇动(如果可用),但是我敢肯定Typescript进行必要的检查会更加容易,因为必须将功能导入同一文件中。 (that said, I've been using the old fashion method for a while and VSC was working as expected). (也就是说,一段时间以来,我一直在使用旧的时尚方法,而VSC仍在按预期方式工作)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM