Argument of type 'SPHttpClient' is not assignable to parameter of type 'SPHttpClient'
- [x ] Question
- [ ] Typo
- [ ] Bug
- [ ] Additional article idea
I try to decouple SPHttpClient form WebPart class. Therefore I wrote a class "SampleService", which accepts a parameter of type SPHttpClient. In my webpart class I try to create a new instance of the class by let service: SPHttpService = new SampleService(this.context.spHttpClient), where this is my webpart instance. I get a ts error saying "Argument of type 'SPHttpClient' is not assignable to parameter of type 'SPHttpClient (...) separate declarations of (...) _digestCache".
Further what is best practice to decouple SPHttpService from WebPart, without passing whole context to custom service?
// QuickNavConfig
import { SPHttpClient } from "@microsoft/sp-http";
export interface IQuickNavConfiguration {
spHttpClient: SPHttpClient;
siteAbsoluteUrl: string;
}
// QuickNavService
constructor(config: IQuickNavConfiguration) {
this.httpClient = config.spHttpClient;
this.siteAbsoluteUrl = config.siteAbsoluteUrl;
}
// QuickNavWebPart
let service: QuickNavService = new QuickNavService({
spHttpClient: this.context.spHttpClient,
siteAbsoluteUrl: this.context.pageContext.web.absoluteUrl
});
gulp serve fails with following message: error TS2345: Argument of type '{ spHttpClient: SPHttpClient; siteAbsoluteUrl: string; }' is not assignable to parameter of type 'IQuickNavConfiguration'
Instead of passing the sphttpclient from webpart context I used the serviceScope member to instiate my service class.
thanks
Hi @Fabske411, Could you please tell me how you fixed in more details? i am also getting this error. Thanks!
//import your web part context import { WebPartContext } from "@microsoft/sp-webpart-base"; import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from "@microsoft/sp-http";
//add type in your class private webpartContext:WebPartContext;
//use it to call spHttpClient this.webpartContext.spHttpClient
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues