为什么此代码中存在类型转换错误?

我不明白为什么行this.claim = claim;出现错误“无法将类型'A'转换为类型'声明'”。我需要返回一个observable,因为此函数在解析器中调用。我实际上不需要返回声明本身,我只需要在ClaimStoreService上设置属性,但是我不知道如何返回可观察到的空值或空值。

export class ClaimStoreService {

    constructor(private readonly claimService: ClaimService) { }

    claim: Claim;

    getclaim(guid: string): Observable<Claim> {
        return this.claimService.get(guid).pipe(
            take(1),mergeMap(claim => {
                // Cannot convert type 'A' to type 'Claim'
                this.claim = claim;
                return of(claim);
            }));
    }
}
ilovegina 回答:为什么此代码中存在类型转换错误?

claim: Claim;

getClaim(guid: string): Observable<Claim> {
        return this.claimService.get(guid).pipe(
            take(1),// give type here will solve your issue
            mergeMap((claim:Claim) => {
                this.claim = claim;
                return of(claim);
            }));
    }
,

我认为,如果您使用this.claimService.get<Claim>(guid),则不必在claim中输入mergeMap(claim =>

本文链接:https://www.f2er.com/3150720.html

大家都在问