在从map
返回数组并将其提供给filter
链中的rxjs
时,我迷上了类型。所以这是一个例子:
export interface SunInterface {
brightness: number;
}
export interface MoonInterface {
people: string[];
}
export class HelloComponent {
constructor() {
const first_obs$: Observable<SunInterface> = of({ brightness: 100 });
const second_obs$: Observable<MoonInterface> = Observable.create((observer) => {
setTimeout(() => {
observer.next({ people: ['Neil Armstrong', 'Buzz Aldrin', 'Alan Bean'] });
}, 2000)
});
combineLatest(
first_obs$,
second_obs$
)
.pipe(
map(([sun, moon]) => {
const filtred = moon.people.filter(astronaut => astronaut.startsWith('N'));
return [sun, { ...moon, ...{ people: filtred }}]; // should I add type for arguments?
}),
filter(([sun, moon]) => {
console.log('1111', sun);
console.log('2222', moon);
return !!moon.people.length; // here I get a lint error
})
)
.subscribe(([sun, moon]) => {
})
}
}
我在代码Property 'people' does not exist on type SunInterface | { people: string[] }
中留下注释的地方出现了一个皮棉错误。我猜我在类型方面做错了。有人可以帮我吗?
答案 0 :(得分:0)
尝试在管道中定义type
.....filter(([sun, moon]: [SunInterface, MoonInterface]) => ..... )