diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4423b574e31bc..383aea994cda5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8540,7 +8540,7 @@ namespace ts { if (flags & TypeFlags.Void) types.push(voidType); if (flags & TypeFlags.Undefined) types.push(undefinedType); if (flags & TypeFlags.Null) types.push(nullType); - return getUnionType(types, /*subtypeReduction*/ true); + return getUnionType(types); } function removeDefinitelyFalsyTypes(type: Type): Type { diff --git a/tests/baselines/reference/optionalParameterRetainsNull.js b/tests/baselines/reference/optionalParameterRetainsNull.js new file mode 100644 index 0000000000000..b8fb2c1235051 --- /dev/null +++ b/tests/baselines/reference/optionalParameterRetainsNull.js @@ -0,0 +1,14 @@ +//// [optionalParameterRetainsNull.ts] +interface Bar { bar: number; foo: object | null; } + +let a = { + test (a: K, b?: Bar[K] | null) { } +}; +a.test("bar", null); // ok, null is assignable to number | null | undefined + + +//// [optionalParameterRetainsNull.js] +var a = { + test: function (a, b) { } +}; +a.test("bar", null); // ok, null is assignable to number | null | undefined diff --git a/tests/baselines/reference/optionalParameterRetainsNull.symbols b/tests/baselines/reference/optionalParameterRetainsNull.symbols new file mode 100644 index 0000000000000..95786c1bdcac3 --- /dev/null +++ b/tests/baselines/reference/optionalParameterRetainsNull.symbols @@ -0,0 +1,25 @@ +=== tests/cases/compiler/optionalParameterRetainsNull.ts === +interface Bar { bar: number; foo: object | null; } +>Bar : Symbol(Bar, Decl(optionalParameterRetainsNull.ts, 0, 0)) +>bar : Symbol(Bar.bar, Decl(optionalParameterRetainsNull.ts, 0, 15)) +>foo : Symbol(Bar.foo, Decl(optionalParameterRetainsNull.ts, 0, 29)) + +let a = { +>a : Symbol(a, Decl(optionalParameterRetainsNull.ts, 2, 3)) + + test (a: K, b?: Bar[K] | null) { } +>test : Symbol(test, Decl(optionalParameterRetainsNull.ts, 2, 9)) +>K : Symbol(K, Decl(optionalParameterRetainsNull.ts, 3, 7)) +>Bar : Symbol(Bar, Decl(optionalParameterRetainsNull.ts, 0, 0)) +>a : Symbol(a, Decl(optionalParameterRetainsNull.ts, 3, 29)) +>K : Symbol(K, Decl(optionalParameterRetainsNull.ts, 3, 7)) +>b : Symbol(b, Decl(optionalParameterRetainsNull.ts, 3, 34)) +>Bar : Symbol(Bar, Decl(optionalParameterRetainsNull.ts, 0, 0)) +>K : Symbol(K, Decl(optionalParameterRetainsNull.ts, 3, 7)) + +}; +a.test("bar", null); // ok, null is assignable to number | null | undefined +>a.test : Symbol(test, Decl(optionalParameterRetainsNull.ts, 2, 9)) +>a : Symbol(a, Decl(optionalParameterRetainsNull.ts, 2, 3)) +>test : Symbol(test, Decl(optionalParameterRetainsNull.ts, 2, 9)) + diff --git a/tests/baselines/reference/optionalParameterRetainsNull.types b/tests/baselines/reference/optionalParameterRetainsNull.types new file mode 100644 index 0000000000000..30b7d61f76cba --- /dev/null +++ b/tests/baselines/reference/optionalParameterRetainsNull.types @@ -0,0 +1,31 @@ +=== tests/cases/compiler/optionalParameterRetainsNull.ts === +interface Bar { bar: number; foo: object | null; } +>Bar : Bar +>bar : number +>foo : object | null +>null : null + +let a = { +>a : { test(a: K, b?: Bar[K] | null | undefined): void; } +>{ test (a: K, b?: Bar[K] | null) { }} : { test(a: K, b?: Bar[K] | null | undefined): void; } + + test (a: K, b?: Bar[K] | null) { } +>test : (a: K, b?: Bar[K] | null | undefined) => void +>K : K +>Bar : Bar +>a : K +>K : K +>b : Bar[K] | null | undefined +>Bar : Bar +>K : K +>null : null + +}; +a.test("bar", null); // ok, null is assignable to number | null | undefined +>a.test("bar", null) : void +>a.test : (a: K, b?: Bar[K] | null | undefined) => void +>a : { test(a: K, b?: Bar[K] | null | undefined): void; } +>test : (a: K, b?: Bar[K] | null | undefined) => void +>"bar" : "bar" +>null : null + diff --git a/tests/cases/compiler/optionalParameterRetainsNull.ts b/tests/cases/compiler/optionalParameterRetainsNull.ts new file mode 100644 index 0000000000000..a3b50a82d2cdd --- /dev/null +++ b/tests/cases/compiler/optionalParameterRetainsNull.ts @@ -0,0 +1,7 @@ +// @strictNullChecks: true +interface Bar { bar: number; foo: object | null; } + +let a = { + test (a: K, b?: Bar[K] | null) { } +}; +a.test("bar", null); // ok, null is assignable to number | null | undefined