Skip to content

Commit 0652298

Browse files
authored
Merge pull request #13930 from Microsoft/no-subtype-reduction-in-includeFalsyTypes
No subtype reduction in includeFalsyTypes
2 parents bc1058e + e03509a commit 0652298

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8592,7 +8592,7 @@ namespace ts {
85928592
if (flags & TypeFlags.Void) types.push(voidType);
85938593
if (flags & TypeFlags.Undefined) types.push(undefinedType);
85948594
if (flags & TypeFlags.Null) types.push(nullType);
8595-
return getUnionType(types, /*subtypeReduction*/ true);
8595+
return getUnionType(types);
85968596
}
85978597

85988598
function removeDefinitelyFalsyTypes(type: Type): Type {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [optionalParameterRetainsNull.ts]
2+
interface Bar { bar: number; foo: object | null; }
3+
4+
let a = {
5+
test<K extends keyof Bar> (a: K, b?: Bar[K] | null) { }
6+
};
7+
a.test("bar", null); // ok, null is assignable to number | null | undefined
8+
9+
10+
//// [optionalParameterRetainsNull.js]
11+
var a = {
12+
test: function (a, b) { }
13+
};
14+
a.test("bar", null); // ok, null is assignable to number | null | undefined
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/optionalParameterRetainsNull.ts ===
2+
interface Bar { bar: number; foo: object | null; }
3+
>Bar : Symbol(Bar, Decl(optionalParameterRetainsNull.ts, 0, 0))
4+
>bar : Symbol(Bar.bar, Decl(optionalParameterRetainsNull.ts, 0, 15))
5+
>foo : Symbol(Bar.foo, Decl(optionalParameterRetainsNull.ts, 0, 29))
6+
7+
let a = {
8+
>a : Symbol(a, Decl(optionalParameterRetainsNull.ts, 2, 3))
9+
10+
test<K extends keyof Bar> (a: K, b?: Bar[K] | null) { }
11+
>test : Symbol(test, Decl(optionalParameterRetainsNull.ts, 2, 9))
12+
>K : Symbol(K, Decl(optionalParameterRetainsNull.ts, 3, 7))
13+
>Bar : Symbol(Bar, Decl(optionalParameterRetainsNull.ts, 0, 0))
14+
>a : Symbol(a, Decl(optionalParameterRetainsNull.ts, 3, 29))
15+
>K : Symbol(K, Decl(optionalParameterRetainsNull.ts, 3, 7))
16+
>b : Symbol(b, Decl(optionalParameterRetainsNull.ts, 3, 34))
17+
>Bar : Symbol(Bar, Decl(optionalParameterRetainsNull.ts, 0, 0))
18+
>K : Symbol(K, Decl(optionalParameterRetainsNull.ts, 3, 7))
19+
20+
};
21+
a.test("bar", null); // ok, null is assignable to number | null | undefined
22+
>a.test : Symbol(test, Decl(optionalParameterRetainsNull.ts, 2, 9))
23+
>a : Symbol(a, Decl(optionalParameterRetainsNull.ts, 2, 3))
24+
>test : Symbol(test, Decl(optionalParameterRetainsNull.ts, 2, 9))
25+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/compiler/optionalParameterRetainsNull.ts ===
2+
interface Bar { bar: number; foo: object | null; }
3+
>Bar : Bar
4+
>bar : number
5+
>foo : object | null
6+
>null : null
7+
8+
let a = {
9+
>a : { test<K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined): void; }
10+
>{ test<K extends keyof Bar> (a: K, b?: Bar[K] | null) { }} : { test<K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined): void; }
11+
12+
test<K extends keyof Bar> (a: K, b?: Bar[K] | null) { }
13+
>test : <K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined) => void
14+
>K : K
15+
>Bar : Bar
16+
>a : K
17+
>K : K
18+
>b : Bar[K] | null | undefined
19+
>Bar : Bar
20+
>K : K
21+
>null : null
22+
23+
};
24+
a.test("bar", null); // ok, null is assignable to number | null | undefined
25+
>a.test("bar", null) : void
26+
>a.test : <K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined) => void
27+
>a : { test<K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined): void; }
28+
>test : <K extends "bar" | "foo">(a: K, b?: Bar[K] | null | undefined) => void
29+
>"bar" : "bar"
30+
>null : null
31+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @strictNullChecks: true
2+
interface Bar { bar: number; foo: object | null; }
3+
4+
let a = {
5+
test<K extends keyof Bar> (a: K, b?: Bar[K] | null) { }
6+
};
7+
a.test("bar", null); // ok, null is assignable to number | null | undefined

0 commit comments

Comments
 (0)