Skip to content

Commit f6cef9b

Browse files
jespinohahmadia
andcommitted
Fixes microsoft#32798: Narrow unkown to null|unknown type on == null guard
Co-authored-by: Hossein <[email protected]>
1 parent 541e553 commit f6cef9b

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -23686,6 +23686,9 @@ namespace ts {
2368623686
assumeTrue = !assumeTrue;
2368723687
}
2368823688
const valueType = getTypeOfExpression(value);
23689+
if ((type.flags & TypeFlags.Unknown) && (operator === SyntaxKind.EqualsEqualsToken) && (valueType.flags & TypeFlags.Null)) {
23690+
return getUnionType([nullType, undefinedType]);
23691+
}
2368923692
if ((type.flags & TypeFlags.Unknown) && assumeTrue && (operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken)) {
2369023693
if (valueType.flags & (TypeFlags.Primitive | TypeFlags.NonPrimitive)) {
2369123694
return valueType;

tests/baselines/reference/narrowByEquality.errors.txt

+7
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,11 @@ tests/cases/compiler/narrowByEquality.ts(54,9): error TS2322: Type 'string | num
6868
}
6969
return 0;
7070
}
71+
72+
// From issue #32798
73+
declare let xUnknown: unknown
74+
75+
if (xUnknown == null) {
76+
xUnknown;
77+
}
7178

tests/baselines/reference/narrowByEquality.js

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ function test(level: number | string):number {
5656
}
5757
return 0;
5858
}
59+
60+
// From issue #32798
61+
declare let xUnknown: unknown
62+
63+
if (xUnknown == null) {
64+
xUnknown;
65+
}
5966

6067

6168
//// [narrowByEquality.js]
@@ -99,3 +106,6 @@ function test(level) {
99106
}
100107
return 0;
101108
}
109+
if (xUnknown == null) {
110+
xUnknown;
111+
}

tests/baselines/reference/narrowByEquality.symbols

+11
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,14 @@ function test(level: number | string):number {
111111
return 0;
112112
}
113113

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

tests/baselines/reference/narrowByEquality.types

+13
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,16 @@ function test(level: number | string):number {
130130
>0 : 0
131131
}
132132

133+
// From issue #32798
134+
declare let xUnknown: unknown
135+
>xUnknown : unknown
136+
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

+7
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ function test(level: number | string):number {
5757
}
5858
return 0;
5959
}
60+
61+
// From issue #32798
62+
declare let xUnknown: unknown
63+
64+
if (xUnknown == null) {
65+
xUnknown;
66+
}

0 commit comments

Comments
 (0)