fix(common): correct and simplify typing of I18nPluralPipe (#37447)

I18nPluralPipe can actually accept `null` and `undefined` (which are
convenient for composing it with the async pipe), but it is currently
typed to only accept `number`.

PR Close #37447
This commit is contained in:
Andrea Canciani 2020-03-27 11:44:00 +01:00 committed by Alex Rickabaugh
parent 7b2aac97df
commit 3b919ef10f
4 changed files with 11 additions and 5 deletions

View File

@ -110,7 +110,7 @@ export declare class HashLocationStrategy extends LocationStrategy {
export declare class I18nPluralPipe implements PipeTransform {
constructor(_localization: NgLocalization);
transform(value: number, pluralMap: {
transform(value: number | null | undefined, pluralMap: {
[count: string]: string;
}, locale?: string): string;
}

View File

@ -39,7 +39,8 @@ export class I18nPluralPipe implements PipeTransform {
* @param locale a `string` defining the locale to use (uses the current {@link LOCALE_ID} by
* default).
*/
transform(value: number, pluralMap: {[count: string]: string}, locale?: string): string {
transform(value: number|null|undefined, pluralMap: {[count: string]: string}, locale?: string):
string {
if (value == null) return '';
if (typeof pluralMap !== 'object' || pluralMap === null) {

View File

@ -54,12 +54,17 @@ import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_refle
});
it('should use "" if value is undefined', () => {
const val = pipe.transform(void (0) as any, mapping);
const val = pipe.transform(undefined, mapping);
expect(val).toEqual('');
});
it('should use "" if value is null', () => {
const val = pipe.transform(null, mapping);
expect(val).toEqual('');
});
it('should not support bad arguments', () => {
expect(() => pipe.transform(0, <any>'hey')).toThrowError();
expect(() => pipe.transform(0, 'hey' as any)).toThrowError();
});
});
});

View File

@ -40,7 +40,7 @@ import {JitReflector} from '@angular/platform-browser-dynamic/src/compiler_refle
});
it('should throw on bad arguments', () => {
expect(() => pipe.transform('male', <any>'hey')).toThrowError();
expect(() => pipe.transform('male', 'hey' as any)).toThrowError();
});
});
});