21

I just made two important upgrades to our Angular 4 application and build tools:

  1. @angular/core ^4.1.3 => ^4.2.4 (and /http, /forms, etc)
  2. tslint ^5.3.2 => ^5.4.3

I have a Service which declares options like so:

@Injectable()
export class WorkOrderService {

    private headers: Headers = new Headers({ 'Content-Type': 'application/json' });
    private options: RequestOptions = new RequestOptions(this.headers);

    constructor(private http: Http) {}

    /* Methods ... */
}

The above now no longer validates tslint, throwing the following error:

error TS2559: Type 'Headers' has no properties in common with type 'RequestOptionsArgs'.

The source (@angular/http interface.d.ts:43) clearly allows for Headers as a RequestOptionsArgs:

/**
 * Interface for options to construct a RequestOptions, based on
 * [RequestInit](https://fetch.spec.whatwg.org/#requestinit) from the Fetch spec.
 *
 * @experimental
 */
export interface RequestOptionsArgs {
    url?: string | null;
    method?: string | RequestMethod | null;
    /** @deprecated from 4.0.0. Use params instead. */
    search?: string | URLSearchParams | {
        [key: string]: any | any[];
    } | null;
    params?: string | URLSearchParams | {
        [key: string]: any | any[];
    } | null;
    headers?: Headers | null;
    body?: any;
    withCredentials?: boolean | null;
    responseType?: ResponseContentType | null;
}

1 Answer 1

33

Update for 4.3 HttpClient

The new syntax to be compatible with HttpClient, introduced in angular 4.3, is:

import { HttpClient, HttpHeaders } from "@angular/common/http";

private _options = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };

No more RequestOptions: parameters are added using the new immutable HttpParams Map.

Pre 4.3 / Http

I just noticed that RequestOptions now requires you to explicitly pass named options as an object, like:

headers: Headers = new Headers({ 'Content-Type': 'application/json' });
options: RequestOptions = new RequestOptions({ headers: this.headers });
15
  • 1
    i have being running into similar issue , thank's for providing a solution Jun 23, 2017 at 19:39
  • 2
    @LLai Object destructuring doesn't work in this case because of the this.: Argument of type '{ this: any; }' is not assignable to parameter of type 'RequestOptionsArgs'. Object literal may only specify known properties, and 'this' does not exist in type 'RequestOptionsArgs'. but I had hoped so, as well.
    – msanford
    Jun 23, 2017 at 19:44
  • 1
    @AkshayVijayJain You're mixing paradigms. HttpClient doesn't come from @angular/http, it comes from @angular/common/http, which is where the rest are exported from as well. Updated the post.
    – msanford
    Nov 2, 2017 at 18:28
  • 2
    Thank you so much @msanford, however I am getting this error Argument of type '{ headers: HttpHeaders; }' is not assignable to parameter of type 'RequestOptionsArgs'. Types of property 'headers' are incompatible. Type 'HttpHeaders' is not assignable to type 'Headers'. Property 'forEach' is missing in type 'HttpHeaders'.' , when i am requesting like this ---->this.http.post('localhost:56451/map', { "username": username, "password": password }, this._options) Nov 2, 2017 at 18:29
  • 1
    @AkshayVijayJain My pleasure. Again, you are mixing paradigms. I left RequestOptionsArgs in the second part of the post because it no longer applies to HttpClient. The two complete code portions are equivalent. If you have a more complex case (such as setting request parameters) I'd post a new question.
    – msanford
    Nov 2, 2017 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.