我的Angular RxJS combineLatest没有将类型传播到pipe操作符。

huangapple go评论73阅读模式
英文:

My angular rxjs combineLatest is not propagating the types to the pipe operator

问题

The IDE properly identifies the two objects in the combineLatest operator as an IStory[] and string respectively. Once I pipe and destructure the content, the IDE identifies both elements as storys:unknown & selectedId:unknown. The IDE crashes with type errors when I run ng start

The code combines an Array<IStory> and a single string element and is filtered to return the IStory with a matching id value and assign it to an Observerable<IStory>.

activeStory$: Observable<IStory> = combineLatest([
        this.backend.stories$,  // IStory数组
        this.backend.selectedIdAction$       // 文本字符串
    ]).pipe(
        map(([storys, selectedId]) =>     // 数组解构
          storys.filter(story => story.id === selectedId)
      ));
英文:

The IDE properly identifies the two objects in the combineLatest operator as an IStory[] and string respectively. Once I pipe and destructure the content, the IDE identifies both elements as storys:unknown & selectedId:unknown. The IDE crashes with type errors when I run ng start

The code combines an Array<IStory> and a single string element and is filtered to return the IStory with a matching id value and assign it to an Observerable<IStory>.

activeStory$:Observable&lt;IStory&gt; = combineLatest([          
        this.backend.stories$,  //array of IStory                               
        this.backend.selectedIdAction$       // a text string                  
    ]).pipe(                                                   
        map(([storys,selectedId]) =&gt;     //array destructuring                       
          storys.filter(story =&gt; story.id === selectedId)      
      ));     

答案1

得分: 1

我没有解决类型不匹配的问题,但在使用一些虚拟代码在MDN上测试过滤函数后,我意识到我返回了一个数组。
数组 [对象 {名称:“喷雾”,ID:“asdf”}]
虽然不识别解构元素,但IDE抱怨将Array&lt;IStory&gt;分配给IStory。我的工作解决方案是取索引零:

storys.filter(story =&gt; story.id === selectedId)[0]

英文:

I did not resolve the type mismatch, but after testing the filter function on MDN with some dummy code, I realized I was returning an array.
Array [Object { name: "spray", id: "asdf" }]
The IDE was complaining about assigning an Array&lt;IStory&gt; to an IStory despite not recognizing the destructured elements. My working solution was to take index zero:

storys.filter(story =&gt; story.id === selectedId)[0]

huangapple
  • 本文由 发表于 2023年7月14日 04:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定