简体   繁体   English

RXJS Observable.of()作为Observable <HttpEvent<any> &gt;在HttpInterceptor中

[英]RXJS Observable.of() as a Observable<HttpEvent<any>> in HttpInterceptor

I have an Angular's HttpInterceptor looking like that: 我有一个Angular的HttpInterceptor看起来像这样:

(...)

export class CacheInterceptor implements HttpInterceptor {

  constructor(private cache: CacheService) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

   const cachedResponse = this.cache.restore(req); // Object with cached data

   return cachedResponse ?
     of(cachedResponse): this.sendRequest(req, next);

(...)
}

Generally speaking it seems to work in the end but Angular's compiler is complaining showing error about types not matching: 一般来说,它似乎最后可以工作,但是Angular的编译器抱怨显示出类型不匹配的错误:

ERROR in cache.interceptor.ts(27,5): error TS2322: >Type 'Observable> | cache.interceptor.ts(27,5)中的错误:错误TS2322:>键入“可观察的”> | Observable' is not assignable to >type 'Observable>'. “可观察”不能分配给>“可观察>”类型。 Type 'Observable' is not assignable to type >'Observable>'. 类型“可观察”不能分配给类型>“可观察>”。 Type 'string' is not assignable to type 'HttpEvent'. 类型“ string”不可分配给类型“ HttpEvent”。

Almost the same example can be found in the Angular Docs. 在Angular Docs中几乎可以找到相同的示例。

The problem here is that of(cachedResponse) is not a type Observable<HttpEvent<any>> but Observable<string> . 这里的问题是of(cachedResponse)不是Observable<HttpEvent<any>>而是Observable<string>

I tried many ways but I can't think of what would be the best solution for this. 我尝试了很多方法,但我想不出什么是最好的解决方案。 Is it possible in any way to convert cached data to Observable<HttpEvent<any>> type or possibly there is another good way of fixing it? 是否有可能以任何方式将缓存的数据转换为Observable<HttpEvent<any>>类型,或者可能还有另一种好的方法来对其进行修复? I know I could provide my own HttpInterceptor interface with proper types for my case or even simply remove HttpInterface reference but these might not be the best solutions, especially tha latter one. 我知道我可以为我的情况提供适合自己的HttpInterceptor接口,甚至可以简单地删除HttpInterface引用,但是这些可能不是最佳解决方案,尤其是后者。 Anybody? 有人吗

If you cannot change the typing of CacheService , which is what your question sounds like to me, then you can just use a type assertion: 如果您不能更改CacheService的类型,这对您来说是我的问题,那么您可以使用类型断言:

const cachedResponse = this.cache.restore(req) as HttpEvent<any>;

Note that a type assertion does not modify the data; 需要注意的是一个类型的断言修改数据; it simply tells the Typescript compiler that you know that the type is what you claim it is. 它只是告诉Typescript编译器您知道类型就是您声明的类型。

Ideally, however, CacheService#restore is defined generically, in which case 但是,理想情况下, CacheService#restore是通用定义的,在这种情况下

const cachedResponse = this.cache.restore<HttpEvent<any>>(req);

would work. 会工作。 Functionally speaking it's exactly the same and makes no difference, but it depends on restore being appropriately typed. 从功能上讲,它是完全相同的,没有任何区别,但它取决于要正确键入的restore Since I don't know what CacheService is in your case, I can't know whether that's the case. 由于我不知道您使用的CacheService是什么,所以我不知道情况是否如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rxjs 错误类型 &#39;Observable <HttpEvent<any> &gt;&#39; - Rxjs error type 'Observable<HttpEvent<any>>' 压扁 Promise <observable<httpevent<any> &gt;&gt; 到可观察的<httpevent<any> &gt; </httpevent<any></observable<httpevent<any> - Squashing Promise<Observable<HttpEvent<any>>> to Observable<HttpEvent<any>> 类型&#39;Observable &lt;{}&gt;不可分配给类型&#39;Observable <HttpEvent<any> &gt;” - Type 'Observable<{}> is not assignable to type 'Observable<HttpEvent<any>>' 如何订阅和测试 Observable.of rxjs Angular 9 - How to subscribe and test Observable.of rxjs Angular 9 用于Observable.of()的RxJS first() - 没有顺序的元素 - RxJS first() for Observable.of() - no elements in sequence 类型&#39;Observable &lt;{}不可分配给类型&#39;Observable <HttpEvent<any> &gt;” - Type 'Observable<{} is not assignable to type 'Observable<HttpEvent<any>>' 无法在RxJs 6和Angular 6中使用Observable.of - Could not use Observable.of in RxJs 6 and Angular 6 &#39;可观察的<Observable<HttpEvent<any> &gt;&gt;&#39; 不可分配到类型 &#39;Observable<any[]> &#39; - 'Observable<Observable<HttpEvent<any>>>' is not assignable to type 'Observable<any[]>' rxjs 6 可观察<any>可观察的<any[]></any[]></any> - rxjs 6 observable<any> to observable<any[]> Angular 10 类型 &#39;Observable<unknown> &#39; 不可分配给类型 &#39;Observable <HttpEvent<any> &gt;&#39; - Angular 10 Type 'Observable<unknown>' is not assignable to type 'Observable<HttpEvent<any>>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM