Skip to content

Commit e8d9bc5

Browse files
JeanMecheAndrewKushnir
authored andcommitted
refactor(http): rework the HttpEvent union to improve narrowing. (#63267)
Prior to this change, `HttpProgressEvent` could not be narrowed to `HttpDownloadProgressEvent` or `HttpUploadProgressEvent` PR Close #63267
1 parent 26072bb commit e8d9bc5

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

goldens/public-api/common/http/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,7 @@ export class HttpErrorResponse extends HttpResponseBase implements Error {
25202520
}
25212521

25222522
// @public
2523-
export type HttpEvent<T> = HttpSentEvent | HttpHeaderResponse | HttpResponse<T> | HttpProgressEvent | HttpUserEvent<T>;
2523+
export type HttpEvent<T> = HttpSentEvent | HttpHeaderResponse | HttpResponse<T> | HttpDownloadProgressEvent | HttpUploadProgressEvent | HttpUserEvent<T>;
25242524

25252525
// @public
25262526
export enum HttpEventType {

packages/common/http/src/response.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ export type HttpEvent<T> =
144144
| HttpSentEvent
145145
| HttpHeaderResponse
146146
| HttpResponse<T>
147-
| HttpProgressEvent
147+
| HttpDownloadProgressEvent
148+
| HttpUploadProgressEvent
148149
| HttpUserEvent<T>;
149150

150151
/**

packages/common/http/test/response_spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {HttpHeaders} from '../src/headers';
10-
import {HttpResponse, HttpStatusCode} from '../src/response';
10+
import {HttpEvent, HttpEventType, HttpResponse, HttpStatusCode} from '../src/response';
1111

1212
describe('HttpResponse', () => {
1313
describe('constructor()', () => {
@@ -94,4 +94,18 @@ describe('HttpResponse', () => {
9494
expect(clone.redirected).toBe(false);
9595
});
9696
});
97+
98+
describe('typings', () => {
99+
it('should correctly narrow based on the type', () => {
100+
const httpEvent: HttpEvent<any> = {
101+
type: HttpEventType.DownloadProgress,
102+
loaded: 100,
103+
total: 200,
104+
};
105+
106+
if (httpEvent.type === HttpEventType.DownloadProgress) {
107+
const partialText: string | undefined = httpEvent.partialText;
108+
}
109+
});
110+
});
97111
});

0 commit comments

Comments
 (0)