Skip to content

Fix GH-32798: Allow == null to narrow unknown to null | undefined #45853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 22, 2021
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
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23895,6 +23895,9 @@ namespace ts {
assumeTrue = !assumeTrue;
}
const valueType = getTypeOfExpression(value);
if (assumeTrue && (type.flags & TypeFlags.Unknown) && (operator === SyntaxKind.EqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsToken) && (valueType.flags & TypeFlags.Null)) {
return getUnionType([nullType, undefinedType]);
}
if ((type.flags & TypeFlags.Unknown) && assumeTrue && (operator === SyntaxKind.EqualsEqualsEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken)) {
if (valueType.flags & (TypeFlags.Primitive | TypeFlags.NonPrimitive)) {
return valueType;
Expand Down
19 changes: 17 additions & 2 deletions tests/baselines/reference/narrowByEquality.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/narrowByEquality.ts(53,15): error TS2322: Type 'string | number' is not assignable to type 'number'.
tests/cases/compiler/narrowByEquality.ts(54,15): error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.
tests/cases/compiler/narrowByEquality.ts(54,9): error TS2322: Type 'string | number' is not assignable to type 'number'.
tests/cases/compiler/narrowByEquality.ts(55,9): error TS2322: Type 'string | number' is not assignable to type 'number'.
Type 'string' is not assignable to type 'number'.


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

if (x == n) {
x;
Expand Down Expand Up @@ -68,4 +69,18 @@ tests/cases/compiler/narrowByEquality.ts(54,9): error TS2322: Type 'string | num
}
return 0;
}

// From issue #32798
if (xUnknown == null) {
xUnknown;
} else {
xUnknown
}

if (xUnknown != null) {
xUnknown;
} else {
xUnknown;
}


28 changes: 28 additions & 0 deletions tests/baselines/reference/narrowByEquality.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare let x: number | string | boolean
declare let n: number;
declare let s: string;
declare let b: boolean;
declare let xUnknown: unknown;

if (x == n) {
x;
Expand Down Expand Up @@ -56,6 +57,20 @@ function test(level: number | string):number {
}
return 0;
}

// From issue #32798
if (xUnknown == null) {
xUnknown;
} else {
xUnknown
}

if (xUnknown != null) {
xUnknown;
} else {
xUnknown;
}



//// [narrowByEquality.js]
Expand Down Expand Up @@ -99,3 +114,16 @@ function test(level) {
}
return 0;
}
// From issue #32798
if (xUnknown == null) {
xUnknown;
}
else {
xUnknown;
}
if (xUnknown != null) {
xUnknown;
}
else {
xUnknown;
}
51 changes: 39 additions & 12 deletions tests/baselines/reference/narrowByEquality.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ declare let s: string;
declare let b: boolean;
>b : Symbol(b, Decl(narrowByEquality.ts, 3, 11))

declare let xUnknown: unknown;
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))

if (x == n) {
>x : Symbol(x, Decl(narrowByEquality.ts, 0, 11))
>n : Symbol(n, Decl(narrowByEquality.ts, 1, 11))
Expand Down Expand Up @@ -71,43 +74,67 @@ if (x == false) {
}

declare let xAndObj: number | string | boolean | object
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))

if (xAndObj == {}) {
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))

xAndObj;
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))
}

if (x == xAndObj) {
>x : Symbol(x, Decl(narrowByEquality.ts, 0, 11))
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))

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

xAndObj;
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 37, 11))
>xAndObj : Symbol(xAndObj, Decl(narrowByEquality.ts, 38, 11))
}

// Repro from #24991

function test(level: number | string):number {
>test : Symbol(test, Decl(narrowByEquality.ts, 46, 1))
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
>test : Symbol(test, Decl(narrowByEquality.ts, 47, 1))
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))

if (level == +level) {
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))

const q2: number = level; // error
>q2 : Symbol(q2, Decl(narrowByEquality.ts, 52, 13))
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
>q2 : Symbol(q2, Decl(narrowByEquality.ts, 53, 13))
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))

return level;
>level : Symbol(level, Decl(narrowByEquality.ts, 50, 14))
>level : Symbol(level, Decl(narrowByEquality.ts, 51, 14))
}
return 0;
}

// From issue #32798
if (xUnknown == null) {
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))

xUnknown;
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))

} else {
xUnknown
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))
}

if (xUnknown != null) {
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))

xUnknown;
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))

} else {
xUnknown;
>xUnknown : Symbol(xUnknown, Decl(narrowByEquality.ts, 4, 11))
}


31 changes: 31 additions & 0 deletions tests/baselines/reference/narrowByEquality.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ declare let s: string;
declare let b: boolean;
>b : boolean

declare let xUnknown: unknown;
>xUnknown : unknown

if (x == n) {
>x == n : boolean
>x : string | number | boolean
Expand Down Expand Up @@ -130,3 +133,31 @@ function test(level: number | string):number {
>0 : 0
}

// From issue #32798
if (xUnknown == null) {
>xUnknown == null : boolean
>xUnknown : unknown
>null : null

xUnknown;
>xUnknown : null | undefined

} else {
xUnknown
>xUnknown : unknown
}

if (xUnknown != null) {
>xUnknown != null : boolean
>xUnknown : unknown
>null : null

xUnknown;
>xUnknown : unknown

} else {
xUnknown;
>xUnknown : null | undefined
}


15 changes: 15 additions & 0 deletions tests/cases/compiler/narrowByEquality.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare let x: number | string | boolean
declare let n: number;
declare let s: string;
declare let b: boolean;
declare let xUnknown: unknown;

if (x == n) {
x;
Expand Down Expand Up @@ -57,3 +58,17 @@ function test(level: number | string):number {
}
return 0;
}

// From issue #32798
if (xUnknown == null) {
xUnknown;
} else {
xUnknown
}

if (xUnknown != null) {
xUnknown;
} else {
xUnknown;
}