'unknown[]' 类型的参数不可分配给 > 'OperatorFunction 类型的参数
Argument of type 'unknown[]' is not assignable to parameter of type > 'OperatorFunction
component.ts
initialize()
方法说
Argument of type 'unknown[]' is not assignable to parameter of type
'OperatorFunction<unknown[], unknown>'. Type 'unknown[]' provides no
match for the signature '(source: Observable<unknown[]>):
Observable'
alertsChanged$: Observable<AlertModel[]>;
private initialize(): void {
this.alertsChanged$ = this.alertsService.getAlerts().pipe(
map((res) => res), // here it returns the above error
tap((res) => {
this.setDataForFilters(res); //Argument of type 'unknown' is not assignable to parameter of type 'AlertModel[]'.
}),
);
}
private setDataForFilters(alerts: AlertModel[]): void {}
service.ts
// no issues here
getAlerts(): Observable<AlertModel[]> {
return this.angularFireDatabase
.list<AlertModel>(`groups/${this.groupId}/alerts`)
.valueChanges()
.pipe(
map((res) => orderBy(res, ['timeStamp'], ['desc'])),
first()
);
}
.html
<app-alerts
[alerts]="alertsChanged$ | async"
></app-alerts>
请让我在这里发布问题?
我在这里发现了问题。这是与 Lodash map
和 RxJS map
的冲突。即,由于 Lodash 映射,VS 代码未导入 RxJS 映射,因此出现上述错误。
因为我需要在同一个组件上同时使用它们,所以我就这样做了。
import { finalize, tap, map } from 'rxjs/operators';
import { map as lodashMap } from 'lodash';
如果我们同时使用来自 'rxjs' 和 "rxjs/operators" 的导入,也会出现同样的错误。在我的例子中,这是代码,
this.anApiCall.getAdata(this.id)
.pipe(map(res => this.dataUtil.parseToForm(this.id, res)),
onErrorResumeNext(of(this.dataUtil.newForm(this.id))))
.pipe(tap(frm => this.form = frm))
.subscribe();
我使用的是 'onErrorResumeNext' 运算符,但不幸的是,IDE 从 'rxjs' 而不是 rxjs/operators 导入了函数。
import { onErrorResumeNext } from "rxjs"; // if the import is like this then you will get error like 'Argument of type 'Observable<YourType>' is not assignable to parameter of type 'OperatorFunction<unknown, unknown>'.'
因此请检查导入是否来自 'rxjs/operators'。并将导入更改为
import { map, tap, onErrorResumeNext } from "rxjs/operators";
component.ts
initialize()
方法说
Argument of type 'unknown[]' is not assignable to parameter of type 'OperatorFunction<unknown[], unknown>'. Type 'unknown[]' provides no match for the signature '(source: Observable<unknown[]>): Observable'
alertsChanged$: Observable<AlertModel[]>;
private initialize(): void {
this.alertsChanged$ = this.alertsService.getAlerts().pipe(
map((res) => res), // here it returns the above error
tap((res) => {
this.setDataForFilters(res); //Argument of type 'unknown' is not assignable to parameter of type 'AlertModel[]'.
}),
);
}
private setDataForFilters(alerts: AlertModel[]): void {}
service.ts
// no issues here
getAlerts(): Observable<AlertModel[]> {
return this.angularFireDatabase
.list<AlertModel>(`groups/${this.groupId}/alerts`)
.valueChanges()
.pipe(
map((res) => orderBy(res, ['timeStamp'], ['desc'])),
first()
);
}
.html
<app-alerts
[alerts]="alertsChanged$ | async"
></app-alerts>
请让我在这里发布问题?
我在这里发现了问题。这是与 Lodash map
和 RxJS map
的冲突。即,由于 Lodash 映射,VS 代码未导入 RxJS 映射,因此出现上述错误。
因为我需要在同一个组件上同时使用它们,所以我就这样做了。
import { finalize, tap, map } from 'rxjs/operators';
import { map as lodashMap } from 'lodash';
如果我们同时使用来自 'rxjs' 和 "rxjs/operators" 的导入,也会出现同样的错误。在我的例子中,这是代码,
this.anApiCall.getAdata(this.id)
.pipe(map(res => this.dataUtil.parseToForm(this.id, res)),
onErrorResumeNext(of(this.dataUtil.newForm(this.id))))
.pipe(tap(frm => this.form = frm))
.subscribe();
我使用的是 'onErrorResumeNext' 运算符,但不幸的是,IDE 从 'rxjs' 而不是 rxjs/operators 导入了函数。
import { onErrorResumeNext } from "rxjs"; // if the import is like this then you will get error like 'Argument of type 'Observable<YourType>' is not assignable to parameter of type 'OperatorFunction<unknown, unknown>'.'
因此请检查导入是否来自 'rxjs/operators'。并将导入更改为
import { map, tap, onErrorResumeNext } from "rxjs/operators";