Skip to content

Commit

Permalink
fix(http): move destructuring inside {Request,Response}Options ctor
Browse files Browse the repository at this point in the history
Previously the RequestOptions/ResponseOptions classes had constructors
with a destructured argument hash (represented by the
{Request,Response}OptionsArgs type). This type consists entirely of
optional members.

This produces a .d.ts file which includes the constructor declaration:

constructor({param, otherParam}?: OptionsArgs);

However, this declaration doesn't type-check properly. TypeScript
determines the actual type of the hash parameter to be OptionsArgs | undefined,
which it then concludes does not have a `param` or `otherParam` member.

This is a bug in TypeScript ( microsoft/TypeScript#10078 ).
As a workaround, destructuring is moved inside the method, where it does not produce
broken artifacts in the .d.ts.

Fixes angular#16663.
  • Loading branch information
alxhub committed Jun 9, 2017
1 parent 2d56239 commit 1d7ac0c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
5 changes: 2 additions & 3 deletions packages/http/src/base_request_options.ts
Expand Up @@ -79,9 +79,8 @@ export class RequestOptions {
responseType: ResponseContentType|null;

// TODO(Dzmitry): remove search when this.search is removed
constructor(
{method, headers, body, url, search, params, withCredentials,
responseType}: RequestOptionsArgs = {}) {
constructor(opts: RequestOptionsArgs = {}) {
const {method, headers, body, url, search, params, withCredentials, responseType} = opts;
this.method = method != null ? normalizeMethodName(method) : null;
this.headers = headers != null ? headers : null;
this.body = body != null ? body : null;
Expand Down
3 changes: 2 additions & 1 deletion packages/http/src/base_response_options.ts
Expand Up @@ -65,7 +65,8 @@ export class ResponseOptions {
*/
type: ResponseType|null;
url: string|null;
constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) {
constructor(opts: ResponseOptionsArgs = {}) {
const {body, status, headers, statusText, type, url} = opts;
this.body = body != null ? body : null;
this.status = status != null ? status : null;
this.headers = headers != null ? headers : null;
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/http/http.d.ts
Expand Up @@ -144,7 +144,7 @@ export declare class RequestOptions {
/** @deprecated */ search: URLSearchParams;
url: string | null;
withCredentials: boolean | null;
constructor({method, headers, body, url, search, params, withCredentials, responseType}?: RequestOptionsArgs);
constructor(opts?: RequestOptionsArgs);
merge(options?: RequestOptionsArgs): RequestOptions;
}

Expand Down Expand Up @@ -192,7 +192,7 @@ export declare class ResponseOptions {
headers: Headers | null;
status: number | null;
url: string | null;
constructor({body, status, headers, statusText, type, url}?: ResponseOptionsArgs);
constructor(opts?: ResponseOptionsArgs);
merge(options?: ResponseOptionsArgs): ResponseOptions;
}

Expand Down

0 comments on commit 1d7ac0c

Please sign in to comment.