sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

Argument of type 'SPHttpClient' is not assignable to parameter of type 'SPHttpClient'

Open Fabske411 opened this issue 8 years ago • 5 comments

  • [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?

Fabske411 avatar Apr 10 '18 14:04 Fabske411

// 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'

Fabske411 avatar Apr 10 '18 15:04 Fabske411

Instead of passing the sphttpclient from webpart context I used the serviceScope member to instiate my service class.

thanks

Fabske411 avatar Apr 16 '18 08:04 Fabske411

Hi @Fabske411, Could you please tell me how you fixed in more details? i am also getting this error. Thanks!

NavaneethaDev avatar Mar 14 '19 12:03 NavaneethaDev

//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

CloudDesignBox avatar Jun 05 '19 10:06 CloudDesignBox

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

msft-github-bot avatar Jan 28 '20 00:01 msft-github-bot