Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'Headers' has no properties in common with type 'RequestOptionsArgs'?

Tags:

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;
}
like image 350
msanford Avatar asked Jun 23 '17 19:06

msanford


1 Answers

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 });
like image 112
msanford Avatar answered Oct 04 '22 11:10

msanford