Javascript 使用带有过滤器(布尔值)的RxJS进行查询?

Javascript 使用带有过滤器(布尔值)的RxJS进行查询?,javascript,angular,typescript,rxjs,rxjs6,Javascript,Angular,Typescript,Rxjs,Rxjs6,我正在阅读一些代码片段: search(query: string) { of(query). pipe( filter(Boolean), debounceTime(300), filter(Boolean)与filter(v=>!!v)基本相同吗?是的,它们是相同的 console.log(typeof Boolean); // prints function console.log(Boolean.prototype.constructor("tr

我正在阅读一些代码片段:

search(query: string) {
  of(query).
  pipe(
    filter(Boolean), 
    debounceTime(300), 

filter(Boolean)
filter(v=>!!v)
基本相同吗?

是的,它们是相同的

   console.log(typeof Boolean); // prints function
   console.log(Boolean.prototype.constructor("truthy")); // prints true
   console.log(Boolean === Boolean.prototype.constructor); // prints true
Boolean
全局引用指向构造函数,该构造函数从第一个参数返回布尔值

构造函数可用于创建布尔包装器对象,但它与原语真值不同


参考:

它们实现了相同的结果,即您不会在订阅中获得未定义的值

不同之处在于,使用过滤器(布尔值)时会丢失类型推断


为什么使用过滤器(布尔值)会丢失类型推断?它与TypeScript和/或RxJs有关吗?它是由TypeScript和/或RxJs记录的吗?@Marcus它与TypeScript有关。上面的说法不正确。该类型将被推断为
boolean
,因为您将构造函数作为回调传递,而
filter
类型从回调的第一个参数类型推断类型。这是
布尔值(val:Boolean)
。您可以自己将类型定义为
filter(Boolean)
,它告诉函数可观察类型应该是什么。此外,当原始值实际上是
String
时,上面使用了
String
。这两种类型不是一回事。
    console.log(new Boolean("truthy")); // prints an object.
    console.log(new Boolean("truthy").valueOf() === true); // prints true
    console.log((new Boolean("truthy")) === true); // prints false
    console.log(Boolean("truthy") === true); // prints true
const query = 'the query';

of(query).
  pipe(
     filter(Boolean)
  ).subscribe(val); // val here is of type 'Any'

of(query).
  pipe(
     filter(Boolean)
  ).subscribe((val: string)); // we can infer it back to string later

of(query).
   pipe(
      filter(v=> v!== undefined)
   ).subscribe(val); // val here is of type 'string'