-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix narrowing of intersections of type variables and primitive types (#…
…43131) * Fix narrowing of intersections of type variables and primitive types * Add tests
- Loading branch information
1 parent
ec77bff
commit a21f61f
Showing
6 changed files
with
364 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/baselines/reference/intersectionNarrowing.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
tests/cases/conformance/types/intersection/intersectionNarrowing.ts(36,16): error TS2367: This condition will always return 'false' since the types 'T & number' and 'string' have no overlap. | ||
|
||
|
||
==== tests/cases/conformance/types/intersection/intersectionNarrowing.ts (1 errors) ==== | ||
// Repros from #43130 | ||
|
||
function f1<T>(x: T & string | T & undefined) { | ||
if (x) { | ||
x; // Should narrow to T & string | ||
} | ||
} | ||
|
||
function f2<T>(x: T & string | T & undefined) { | ||
if (x !== undefined) { | ||
x; // Should narrow to T & string | ||
} | ||
else { | ||
x; // Should narrow to T & undefined | ||
} | ||
} | ||
|
||
function f3<T>(x: T & string | T & number) { | ||
if (typeof x === "string") { | ||
x; // Should narrow to T & string | ||
} | ||
else { | ||
x; // Should narrow to T & number | ||
} | ||
} | ||
|
||
function f4<T>(x: T & 1 | T & 2) { | ||
switch (x) { | ||
case 1: x; break; // T & 1 | ||
case 2: x; break; // T & 2 | ||
default: x; // Should narrow to never | ||
} | ||
} | ||
|
||
function f5<T extends string | number>(x: T & number) { | ||
const t1 = x === "hello"; // Should be an error | ||
~~~~~~~~~~~~~ | ||
!!! error TS2367: This condition will always return 'false' since the types 'T & number' and 'string' have no overlap. | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//// [intersectionNarrowing.ts] | ||
// Repros from #43130 | ||
|
||
function f1<T>(x: T & string | T & undefined) { | ||
if (x) { | ||
x; // Should narrow to T & string | ||
} | ||
} | ||
|
||
function f2<T>(x: T & string | T & undefined) { | ||
if (x !== undefined) { | ||
x; // Should narrow to T & string | ||
} | ||
else { | ||
x; // Should narrow to T & undefined | ||
} | ||
} | ||
|
||
function f3<T>(x: T & string | T & number) { | ||
if (typeof x === "string") { | ||
x; // Should narrow to T & string | ||
} | ||
else { | ||
x; // Should narrow to T & number | ||
} | ||
} | ||
|
||
function f4<T>(x: T & 1 | T & 2) { | ||
switch (x) { | ||
case 1: x; break; // T & 1 | ||
case 2: x; break; // T & 2 | ||
default: x; // Should narrow to never | ||
} | ||
} | ||
|
||
function f5<T extends string | number>(x: T & number) { | ||
const t1 = x === "hello"; // Should be an error | ||
} | ||
|
||
|
||
//// [intersectionNarrowing.js] | ||
"use strict"; | ||
// Repros from #43130 | ||
function f1(x) { | ||
if (x) { | ||
x; // Should narrow to T & string | ||
} | ||
} | ||
function f2(x) { | ||
if (x !== undefined) { | ||
x; // Should narrow to T & string | ||
} | ||
else { | ||
x; // Should narrow to T & undefined | ||
} | ||
} | ||
function f3(x) { | ||
if (typeof x === "string") { | ||
x; // Should narrow to T & string | ||
} | ||
else { | ||
x; // Should narrow to T & number | ||
} | ||
} | ||
function f4(x) { | ||
switch (x) { | ||
case 1: | ||
x; | ||
break; // T & 1 | ||
case 2: | ||
x; | ||
break; // T & 2 | ||
default: x; // Should narrow to never | ||
} | ||
} | ||
function f5(x) { | ||
var t1 = x === "hello"; // Should be an error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
=== tests/cases/conformance/types/intersection/intersectionNarrowing.ts === | ||
// Repros from #43130 | ||
|
||
function f1<T>(x: T & string | T & undefined) { | ||
>f1 : Symbol(f1, Decl(intersectionNarrowing.ts, 0, 0)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 2, 12)) | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 2, 15)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 2, 12)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 2, 12)) | ||
|
||
if (x) { | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 2, 15)) | ||
|
||
x; // Should narrow to T & string | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 2, 15)) | ||
} | ||
} | ||
|
||
function f2<T>(x: T & string | T & undefined) { | ||
>f2 : Symbol(f2, Decl(intersectionNarrowing.ts, 6, 1)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 8, 12)) | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 8, 15)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 8, 12)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 8, 12)) | ||
|
||
if (x !== undefined) { | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 8, 15)) | ||
>undefined : Symbol(undefined) | ||
|
||
x; // Should narrow to T & string | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 8, 15)) | ||
} | ||
else { | ||
x; // Should narrow to T & undefined | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 8, 15)) | ||
} | ||
} | ||
|
||
function f3<T>(x: T & string | T & number) { | ||
>f3 : Symbol(f3, Decl(intersectionNarrowing.ts, 15, 1)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 17, 12)) | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 17, 15)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 17, 12)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 17, 12)) | ||
|
||
if (typeof x === "string") { | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 17, 15)) | ||
|
||
x; // Should narrow to T & string | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 17, 15)) | ||
} | ||
else { | ||
x; // Should narrow to T & number | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 17, 15)) | ||
} | ||
} | ||
|
||
function f4<T>(x: T & 1 | T & 2) { | ||
>f4 : Symbol(f4, Decl(intersectionNarrowing.ts, 24, 1)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 26, 12)) | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 26, 15)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 26, 12)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 26, 12)) | ||
|
||
switch (x) { | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 26, 15)) | ||
|
||
case 1: x; break; // T & 1 | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 26, 15)) | ||
|
||
case 2: x; break; // T & 2 | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 26, 15)) | ||
|
||
default: x; // Should narrow to never | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 26, 15)) | ||
} | ||
} | ||
|
||
function f5<T extends string | number>(x: T & number) { | ||
>f5 : Symbol(f5, Decl(intersectionNarrowing.ts, 32, 1)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 34, 12)) | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 34, 39)) | ||
>T : Symbol(T, Decl(intersectionNarrowing.ts, 34, 12)) | ||
|
||
const t1 = x === "hello"; // Should be an error | ||
>t1 : Symbol(t1, Decl(intersectionNarrowing.ts, 35, 9)) | ||
>x : Symbol(x, Decl(intersectionNarrowing.ts, 34, 39)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
=== tests/cases/conformance/types/intersection/intersectionNarrowing.ts === | ||
// Repros from #43130 | ||
|
||
function f1<T>(x: T & string | T & undefined) { | ||
>f1 : <T>(x: (T & string) | (T & undefined)) => void | ||
>x : (T & string) | (T & undefined) | ||
|
||
if (x) { | ||
>x : (T & string) | (T & undefined) | ||
|
||
x; // Should narrow to T & string | ||
>x : T & string | ||
} | ||
} | ||
|
||
function f2<T>(x: T & string | T & undefined) { | ||
>f2 : <T>(x: (T & string) | (T & undefined)) => void | ||
>x : (T & string) | (T & undefined) | ||
|
||
if (x !== undefined) { | ||
>x !== undefined : boolean | ||
>x : (T & string) | (T & undefined) | ||
>undefined : undefined | ||
|
||
x; // Should narrow to T & string | ||
>x : T & string | ||
} | ||
else { | ||
x; // Should narrow to T & undefined | ||
>x : T & undefined | ||
} | ||
} | ||
|
||
function f3<T>(x: T & string | T & number) { | ||
>f3 : <T>(x: (T & string) | (T & number)) => void | ||
>x : (T & string) | (T & number) | ||
|
||
if (typeof x === "string") { | ||
>typeof x === "string" : boolean | ||
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>x : (T & string) | (T & number) | ||
>"string" : "string" | ||
|
||
x; // Should narrow to T & string | ||
>x : T & string | ||
} | ||
else { | ||
x; // Should narrow to T & number | ||
>x : T & number | ||
} | ||
} | ||
|
||
function f4<T>(x: T & 1 | T & 2) { | ||
>f4 : <T>(x: (T & 1) | (T & 2)) => void | ||
>x : (T & 1) | (T & 2) | ||
|
||
switch (x) { | ||
>x : (T & 1) | (T & 2) | ||
|
||
case 1: x; break; // T & 1 | ||
>1 : 1 | ||
>x : T & 1 | ||
|
||
case 2: x; break; // T & 2 | ||
>2 : 2 | ||
>x : T & 2 | ||
|
||
default: x; // Should narrow to never | ||
>x : never | ||
} | ||
} | ||
|
||
function f5<T extends string | number>(x: T & number) { | ||
>f5 : <T extends string | number>(x: T & number) => void | ||
>x : T & number | ||
|
||
const t1 = x === "hello"; // Should be an error | ||
>t1 : boolean | ||
>x === "hello" : boolean | ||
>x : T & number | ||
>"hello" : "hello" | ||
} | ||
|
Oops, something went wrong.