TSLint 烦人的消息
TSLint annoying message
在我的 angular 组件上,我使用了 RxJs 中的两种方法,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);
});
}
}
应用程序工作正常,在执行(构建)时没有错误甚至没有警告消息,即ng serve
,并且运行 浏览器上的应用程序 按预期工作 并且在浏览器控制台上也没有错误消息或警告。
但是,我的 vscode 上有一条奇怪的 TSLint 消息说:
[ts] Property 'debounceTime' does not exist on type 'Observable<any>'.
这有点烦人,因为我有点担心某些我不知道的东西在幕后不起作用。
我在这里错过了什么?
谢谢。
如某些评论中所述,这不是 TSLINT 错误,而是 Typescript 错误。
这里的事情,当你这样做时,你正在修补 Observable
的原型:
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
与其这样做,您可能只想利用自 rxjs v5.5 以来称为 lettable operators
的新功能。它允许您使用一个新的 .pipe
运算符,它将函数作为参数(rxjs 运算符或您自己的)。
因此,请尝试以下代码,而不是您的代码:
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);
});
}
}
通过不修补 Observable
的原型,它将帮助您的捆绑器进行 tree shaking(如果可用),但我相信 Typescript 会更容易进行必要的检查,因为函数将必须导入到同一个文件中。 (也就是说,我一直在使用老式方法一段时间,而 VSC 正在按预期工作)。
在我的 angular 组件上,我使用了 RxJs 中的两种方法,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);
});
}
}
应用程序工作正常,在执行(构建)时没有错误甚至没有警告消息,即ng serve
,并且运行 浏览器上的应用程序 按预期工作 并且在浏览器控制台上也没有错误消息或警告。
但是,我的 vscode 上有一条奇怪的 TSLint 消息说:
[ts] Property 'debounceTime' does not exist on type 'Observable<any>'.
这有点烦人,因为我有点担心某些我不知道的东西在幕后不起作用。
我在这里错过了什么? 谢谢。
如某些评论中所述,这不是 TSLINT 错误,而是 Typescript 错误。
这里的事情,当你这样做时,你正在修补 Observable
的原型:
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
与其这样做,您可能只想利用自 rxjs v5.5 以来称为 lettable operators
的新功能。它允许您使用一个新的 .pipe
运算符,它将函数作为参数(rxjs 运算符或您自己的)。
因此,请尝试以下代码,而不是您的代码:
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);
});
}
}
通过不修补 Observable
的原型,它将帮助您的捆绑器进行 tree shaking(如果可用),但我相信 Typescript 会更容易进行必要的检查,因为函数将必须导入到同一个文件中。 (也就是说,我一直在使用老式方法一段时间,而 VSC 正在按预期工作)。