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
This commit is contained in:
Chuck Jazdzewski 2018-02-12 09:06:17 -08:00 committed by Miško Hevery
parent b333919722
commit be59c3a98c
3 changed files with 38 additions and 6 deletions

View File

@ -85,8 +85,8 @@ export class AsyncPipe implements OnDestroy, PipeTransform {
transform<T>(obj: null): null; transform<T>(obj: null): null;
transform<T>(obj: undefined): undefined; transform<T>(obj: undefined): undefined;
transform<T>(obj: Observable<T>): T|null; transform<T>(obj: Observable<T>|null|undefined): T|null;
transform<T>(obj: Promise<T>): T|null; transform<T>(obj: Promise<T>|null|undefined): T|null;
transform(obj: Observable<any>|Promise<any>|null|undefined): any { transform(obj: Observable<any>|Promise<any>|null|undefined): any {
if (!this._obj) { if (!this._obj) {
if (obj) { if (obj) {

View File

@ -620,12 +620,12 @@ describe('ng type checker', () => {
}); });
}); });
describe('regressions ', () => { describe('core', () => {
const a = (files: MockFiles, options: ng.AngularCompilerOptions = {}) => { const a = (files: MockFiles, options: ng.AngularCompilerOptions = {}) => {
accept(files, {fullTemplateTypeCheck: true, ...options}); accept(files, {fullTemplateTypeCheck: true, ...options});
}; };
// #19905 // Regression #19905
it('should accept an event binding', () => { it('should accept an event binding', () => {
a({ a({
'src/app.component.ts': '', 'src/app.component.ts': '',
@ -655,6 +655,38 @@ describe('ng type checker', () => {
}); });
}); });
describe('common', () => {
const a = (files: MockFiles, options: ng.AngularCompilerOptions = {}) => {
accept(files, {fullTemplateTypeCheck: true, ...options});
};
// Regression #19905
it('should accept a |undefined or |null parameter for async_pipe', () => {
a({
'src/app.component.ts': '',
'src/lib.ts': '',
'src/app.module.ts': `
import {NgModule, Component} from '@angular/core';
import {CommonModule} from '@angular/common';
@Component({
selector: 'comp',
template: '<div>{{ name | async}}</div>'
})
export class MainComp {
name: Promise<string>|undefined;
}
@NgModule({
declarations: [MainComp],
imports: [CommonModule]
})
export class MainModule {}`
});
});
});
describe('with modified quickstart (fullTemplateTypeCheck: false)', () => { describe('with modified quickstart (fullTemplateTypeCheck: false)', () => {
addTests({fullTemplateTypeCheck: false}); addTests({fullTemplateTypeCheck: false});
}); });

View File

@ -5,8 +5,8 @@ export declare const APP_BASE_HREF: InjectionToken<string>;
export declare class AsyncPipe implements OnDestroy, PipeTransform { export declare class AsyncPipe implements OnDestroy, PipeTransform {
constructor(_ref: ChangeDetectorRef); constructor(_ref: ChangeDetectorRef);
ngOnDestroy(): void; ngOnDestroy(): void;
transform<T>(obj: Promise<T>): T | null; transform<T>(obj: Promise<T> | null | undefined): T | null;
transform<T>(obj: Observable<T>): T | null; transform<T>(obj: Observable<T> | null | undefined): T | null;
transform<T>(obj: undefined): undefined; transform<T>(obj: undefined): undefined;
transform<T>(obj: null): null; transform<T>(obj: null): null;
} }