Jamie Ide
Jamie Ide

Reputation: 49301

Why is there a type conversion error in this code?

I don't understand why the line this.claim = claim; has the error "Cannot convert type 'A' to type 'Claim'". I need to return an observable because this function is called in a resolver. I don't actually need to return the claim itself, I just need to set the property on ClaimStoreService, but I couldn't figure out how to return an empty or void observable.

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);
            }));
    }
}

Upvotes: 0

Views: 77

Answers (2)

Thomas Cayne
Thomas Cayne

Reputation: 1495

I think if you use this.claimService.get<Claim>(guid) you don't have to type claim in mergeMap(claim =>.

Upvotes: 0

Palak Jadav
Palak Jadav

Reputation: 1272

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);
            }));
    }

Upvotes: 1

Related Questions