Skip to content

Commit 007547d

Browse files
committed
1 parent 768291f commit 007547d

File tree

6 files changed

+63
-14
lines changed

6 files changed

+63
-14
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -23895,6 +23895,9 @@ namespace ts {
2389523895
assumeTrue = !assumeTrue;
2389623896
}
2389723897
const valueType = getTypeOfExpression(value);
23898+
if ((type.flags & TypeFlags.Unknown) && (operator === SyntaxKind.EqualsEqualsToken) && (valueType.flags & TypeFlags.Null)) {
23899+
return getUnionType([nullType, undefinedType]);
23900+
}
2389823901
if ((type.flags & TypeFlags.Unknown) && assumeTrue && (operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken)) {
2389923902
if (valueType.flags & (TypeFlags.Primitive | TypeFlags.NonPrimitive)) {
2390023903
return valueType;

tests/baselines/reference/narrowByEquality.errors.txt

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
tests/cases/compiler/narrowByEquality.ts(53,15): error TS2322: Type 'string | number' is not assignable to type 'number'.
1+
tests/cases/compiler/narrowByEquality.ts(54,15): error TS2322: Type 'string | number' is not assignable to type 'number'.
22
Type 'string' is not assignable to type 'number'.
3-
tests/cases/compiler/narrowByEquality.ts(54,9): error TS2322: Type 'string | number' is not assignable to type 'number'.
3+
tests/cases/compiler/narrowByEquality.ts(55,9): error TS2322: Type 'string | number' is not assignable to type 'number'.
44
Type 'string' is not assignable to type 'number'.
55

66

@@ -9,6 +9,7 @@ tests/cases/compiler/narrowByEquality.ts(54,9): error TS2322: Type 'string | num
99
declare let n: number;
1010
declare let s: string;
1111
declare let b: boolean;
12+
declare let xUnknown: unknown;
1213

1314
if (x == n) {
1415
x;
@@ -68,4 +69,9 @@ tests/cases/compiler/narrowByEquality.ts(54,9): error TS2322: Type 'string | num
6869
}
6970
return 0;
7071
}
72+
73+
// From issue #32798
74+
if (xUnknown == null) {
75+
xUnknown;
76+
}
7177

tests/baselines/reference/narrowByEquality.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ declare let x: number | string | boolean
33
declare let n: number;
44
declare let s: string;
55
declare let b: boolean;
6+
declare let xUnknown: unknown;
67

78
if (x == n) {
89
x;
@@ -56,6 +57,11 @@ function test(level: number | string):number {
5657
}
5758
return 0;
5859
}
60+
61+
// From issue #32798
62+
if (xUnknown == null) {
63+
xUnknown;
64+
}
5965

6066

6167
//// [narrowByEquality.js]
@@ -99,3 +105,7 @@ function test(level) {
99105
}
100106
return 0;
101107
}
108+
// From issue #32798
109+
if (xUnknown == null) {
110+
xUnknown;
111+
}

tests/baselines/reference/narrowByEquality.symbols

+23-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ declare let s: string;
1111
declare let b: boolean;
1212
>b : Symbol(b, Decl(narrowByEquality.ts, 3, 11))
1313

14+
declare let xUnknown: unknown;
15+
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))
16+
1417
if (x == n) {
1518
>x : Symbol(x, Decl(narrowByEquality.ts, 0, 11))
1619
>n : Symbol(n, Decl(narrowByEquality.ts, 1, 11))
@@ -71,43 +74,51 @@ if (x == false) {
7174
}
7275

7376
declare let xAndObj: number | string | boolean | object
74-
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
77+
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))
7578

7679
if (xAndObj == {}) {
77-
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
80+
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))
7881

7982
xAndObj;
80-
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
83+
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))
8184
}
8285

8386
if (x == xAndObj) {
8487
>x : Symbol(x, Decl(narrowByEquality.ts, 0, 11))
85-
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
88+
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))
8689

8790
x;
8891
>x : Symbol(x, Decl(narrowByEquality.ts, 0, 11))
8992

9093
xAndObj;
91-
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
94+
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))
9295
}
9396

9497
// Repro from #24991
9598

9699
function test(level: number | string):number {
97-
>test : Symbol(test, Decl(narrowByEquality.ts, 46, 1))
98-
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
100+
>test : Symbol(test, Decl(narrowByEquality.ts, 47, 1))
101+
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))
99102

100103
if (level == +level) {
101-
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
102-
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
104+
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))
105+
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))
103106

104107
const q2: number = level; // error
105-
>q2 : Symbol(q2, Decl(narrowByEquality.ts, 52, 13))
106-
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
108+
>q2 : Symbol(q2, Decl(narrowByEquality.ts, 53, 13))
109+
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))
107110

108111
return level;
109-
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
112+
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))
110113
}
111114
return 0;
112115
}
113116

117+
// From issue #32798
118+
if (xUnknown == null) {
119+
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))
120+
121+
xUnknown;
122+
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))
123+
}
124+

tests/baselines/reference/narrowByEquality.types

+13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ declare let s: string;
1111
declare let b: boolean;
1212
>b : boolean
1313

14+
declare let xUnknown: unknown;
15+
>xUnknown : unknown
16+
1417
if (x == n) {
1518
>x == n : boolean
1619
>x : string | number | boolean
@@ -130,3 +133,13 @@ function test(level: number | string):number {
130133
>0 : 0
131134
}
132135

136+
// From issue #32798
137+
if (xUnknown == null) {
138+
>xUnknown == null : boolean
139+
>xUnknown : unknown
140+
>null : null
141+
142+
xUnknown;
143+
>xUnknown : null | undefined
144+
}
145+

tests/cases/compiler/narrowByEquality.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare let x: number | string | boolean
44
declare let n: number;
55
declare let s: string;
66
declare let b: boolean;
7+
declare let xUnknown: unknown;
78

89
if (x == n) {
910
x;
@@ -57,3 +58,8 @@ function test(level: number | string):number {
5758
}
5859
return 0;
5960
}
61+
62+
// From issue #32798
63+
if (xUnknown == null) {
64+
xUnknown;
65+
}

0 commit comments

Comments
 (0)