Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/optionalParameterRetainsNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [optionalParameterRetainsNull.ts]
interface Bar { bar: number; foo: object | null; }

let a = {
test<K extends keyof Bar> (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
25 changes: 25 additions & 0 deletions tests/baselines/reference/optionalParameterRetainsNull.symbols
Original file line number Diff line number Diff line change
@@ -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<K extends keyof Bar> (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))

31 changes: 31 additions & 0 deletions tests/baselines/reference/optionalParameterRetainsNull.types
Original file line number Diff line number Diff line change
@@ -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<K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined): void; }
>{ test<K extends keyof Bar> (a: K, b?: Bar[K] | null) { }} : { test<K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined): void; }

test<K extends keyof Bar> (a: K, b?: Bar[K] | null) { }
>test : <K extends "bar" | "foo">(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 : <K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined) => void
>a : { test<K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined): void; }
>test : <K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined) => void
>"bar" : "bar"
>null : null

7 changes: 7 additions & 0 deletions tests/cases/compiler/optionalParameterRetainsNull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @strictNullChecks: true
interface Bar { bar: number; foo: object | null; }

let a = {
test<K extends keyof Bar> (a: K, b?: Bar[K] | null) { }
};
a.test("bar", null); // ok, null is assignable to number | null | undefined