fix(common): let KeyValuePipe accept type unions with null (#36093)

`KeyValuePipe` currently accepts `null` values as well as `Map`s and a
few others. However, due to the way in which TS overloads work, a type
of `T|null` will not be accepted by `KeyValuePipe`'s signatures, even
though both `T` and `null` individually would be.

To make this work, each signature that accepts some type `T` has been
duplicated with a second one below it that accepts a `T|null` and
includes `null` in its return type.

Fixes #35743

PR Close #36093
This commit is contained in:
JoostK
2020-03-16 22:48:45 +01:00
committed by Misko Hevery
parent aef432384a
commit 407fa42679
3 changed files with 33 additions and 0 deletions

View File

@ -63,6 +63,16 @@ describe('KeyValuePipe', () => {
const transform2 = pipe.transform({1: 3});
expect(transform1 !== transform2).toEqual(true);
});
it('should accept a type union of an object with string keys and null', () => {
let value !: {[key: string]: string} | null;
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform(value)).toEqual(null);
});
it('should accept a type union of an object with number keys and null', () => {
let value !: {[key: number]: string} | null;
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform(value)).toEqual(null);
});
});
describe('Map', () => {
@ -115,6 +125,11 @@ describe('KeyValuePipe', () => {
const transform2 = pipe.transform(new Map([[1, 3]]));
expect(transform1 !== transform2).toEqual(true);
});
it('should accept a type union of a Map and null', () => {
let value !: Map<number, number>| null;
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform(value)).toEqual(null);
});
});
});