Javascript 类型';可观察<;HttpEvent<;T>&燃气轮机';不可分配给类型';可观察<;T>';
问题出在第14行Javascript 类型';可观察<;HttpEvent<;T>&燃气轮机';不可分配给类型';可观察<;T>';,javascript,angular,typescript,Javascript,Angular,Typescript,问题出在第14行 Type 'Observable<HttpEvent<T>>' is not assignable to type 'Observable<T>'. Type 'HttpEvent<T>' is not assignable to type 'T'. 'HttpEvent<T>' is assignable to the constraint of type 'T', but 'T' could be in
Type 'Observable<HttpEvent<T>>' is not assignable to type 'Observable<T>'.
Type 'HttpEvent<T>' is not assignable to type 'T'.
'HttpEvent<T>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
Type 'HttpSentEvent' is not assignable to type 'T'.
'HttpSentEvent' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.ts(2322)
有很多重载,一些重载返回可观察的
,另一些重载返回可观察的
。如果getHttpParams
的返回值是any
,它认为您将得到后者
因此,最基本的解决方案是更具体地说明该方法可以返回什么,例如:
protected getHttpParams(obj: object): {headers: HttpHeaders, params?: HttpParams} { ... }
我想如果返回
getHttpParams
的类型更清晰,例如{headers:HttpHeaders,params?:any}
,它会更快乐。否则,它似乎认为你在改变观察。该死,这就解决了这个问题。怎么做?因为否则它不知道您是否要观察响应,所以它无法在Observable
和Observable
之间进行选择。是因为这个参数:observe?:“body”;?是的,observe
参数用于设置您应该期望的返回类型。
protected getHttpParams(obj: object): {headers: HttpHeaders, params?: HttpParams} { ... }