Skip to content

Commit c6bdc83

Browse files
chuckjazmhevery
authored andcommitted
fix(common): weaken AsyncPipe transform signature (#22169)
The AsyncPipe type signature was changed to allow deferred creation of promises and observalbes that is supported by the implementation by allowing `Promise<T>|null|undefined` and by allowing `Observable<T>|null|undefined`. PR Close #22169
1 parent 1a897e4 commit c6bdc83

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

packages/common/src/pipes/async_pipe.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ export class AsyncPipe implements OnDestroy, PipeTransform {
8585

8686
transform<T>(obj: null): null;
8787
transform<T>(obj: undefined): undefined;
88-
transform<T>(obj: Observable<T>): T|null;
89-
transform<T>(obj: Promise<T>): T|null;
88+
transform<T>(obj: Observable<T>|null|undefined): T|null;
89+
transform<T>(obj: Promise<T>|null|undefined): T|null;
9090
transform(obj: Observable<any>|Promise<any>|null|undefined): any {
9191
if (!this._obj) {
9292
if (obj) {

packages/compiler-cli/test/diagnostics/check_types_spec.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -620,12 +620,12 @@ describe('ng type checker', () => {
620620
});
621621
});
622622

623-
describe('regressions ', () => {
623+
describe('core', () => {
624624
const a = (files: MockFiles, options: ng.AngularCompilerOptions = {}) => {
625625
accept(files, {fullTemplateTypeCheck: true, ...options});
626626
};
627627

628-
// #19905
628+
// Regression #19905
629629
it('should accept an event binding', () => {
630630
a({
631631
'src/app.component.ts': '',
@@ -655,6 +655,38 @@ describe('ng type checker', () => {
655655
});
656656
});
657657

658+
describe('common', () => {
659+
const a = (files: MockFiles, options: ng.AngularCompilerOptions = {}) => {
660+
accept(files, {fullTemplateTypeCheck: true, ...options});
661+
};
662+
663+
// Regression #19905
664+
it('should accept a |undefined or |null parameter for async_pipe', () => {
665+
a({
666+
'src/app.component.ts': '',
667+
'src/lib.ts': '',
668+
'src/app.module.ts': `
669+
import {NgModule, Component} from '@angular/core';
670+
import {CommonModule} from '@angular/common';
671+
672+
@Component({
673+
selector: 'comp',
674+
template: '<div>{{ name | async}}</div>'
675+
})
676+
export class MainComp {
677+
name: Promise<string>|undefined;
678+
}
679+
680+
681+
@NgModule({
682+
declarations: [MainComp],
683+
imports: [CommonModule]
684+
})
685+
export class MainModule {}`
686+
});
687+
});
688+
});
689+
658690
describe('with modified quickstart (fullTemplateTypeCheck: false)', () => {
659691
addTests({fullTemplateTypeCheck: false});
660692
});

tools/public_api_guard/common/common.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export declare const APP_BASE_HREF: InjectionToken<string>;
55
export declare class AsyncPipe implements OnDestroy, PipeTransform {
66
constructor(_ref: ChangeDetectorRef);
77
ngOnDestroy(): void;
8-
transform<T>(obj: Promise<T>): T | null;
9-
transform<T>(obj: Observable<T>): T | null;
8+
transform<T>(obj: Promise<T> | null | undefined): T | null;
9+
transform<T>(obj: Observable<T> | null | undefined): T | null;
1010
transform<T>(obj: undefined): undefined;
1111
transform<T>(obj: null): null;
1212
}

0 commit comments

Comments
 (0)