为什么 rxjs 在涉及到运算符时会抱怨定义为 Type[] 的项目?

Why does rxjs complain about items defined as Type[] when it comes to operators?

我想我在输入端点时可能犯了一个根本性错误。

https://www.learnrxjs.io/operators/transformation/map.html 显示一组对象的示例,这些对象似乎可以与示例中的运算符一起正常工作。

  this.taskService.getResponsibleParties().pipe(
        takeUntil(this.onDestroy$),
        map(({responPartyDesc}) => responPartyDesc)
    ).subscribe(responsibleParties => {
        this.responsableParties = responsibleParties;
    });

getResponsibleParties 定义为 return 值低于

    getResponsibleParties(): Observable<Array<ResponsibleParty>>

从后端返回的大多数项目都是某种类型接口的数组。为什么在这个例子中 Rxjs 会大喊

     map(({responPartyDesc}) => responPartyDesc)

责任方[]没有属性'responPartyDesc'

这与文档中列出的工作方式不完全一样吗?

learn-rxjs map example 中,observable 一次发出一个对象:

const source: Observable<{ name: string, age: number }> = from([
  { name: 'Joe', age: 30 },
  { name: 'Frank', age: 20 },
  { name: 'Ryan', age: 50 }
]);

因此,map 运算符回调可以将参数声明为具有 name 属性:

的对象
const example = source.pipe(map(({ name }) => name));

在你的例子中,observable 发出一个数组(而不是一次一个项目),并且数组本身没有 responPartyDesc 属性。如果要修改数组以仅保留 responPartyDesc 属性,可以使用以下两种语法之一调用 map 运算符中的 Array.map() 方法:

map((values: Array<ResponsibleParty>) => values.map(x => x.responPartyDesc))

map((values: Array<{ responsPartyDesc }>) => values.map(x => x.responPartyDesc))

有关演示,请参阅 this stackblitz