英文:
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<IStory> = combineLatest([
this.backend.stories$, //array of IStory
this.backend.selectedIdAction$ // a text string
]).pipe(
map(([storys,selectedId]) => //array destructuring
storys.filter(story => story.id === selectedId)
));
答案1
得分: 1
我没有解决类型不匹配的问题,但在使用一些虚拟代码在MDN上测试过滤函数后,我意识到我返回了一个数组。
数组 [对象 {名称:“喷雾”,ID:“asdf”}]
虽然不识别解构元素,但IDE抱怨将Array<IStory>
分配给IStory
。我的工作解决方案是取索引零:
storys.filter(story => 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<IStory>
to an IStory
despite not recognizing the destructured elements. My working solution was to take index zero:
storys.filter(story => story.id === selectedId)[0]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论