Skip to content

Commit 0820249

Browse files
committed
Fixing some tests
1 parent 560bc3f commit 0820249

5 files changed

Lines changed: 51 additions & 86 deletions

File tree

tests/cases/conformance/expressions/typeGuards/typeGuardsInConditionalExpression.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,37 @@ function foo(x: number | string) {
1111
: x++; // number
1212
}
1313
function foo2(x: number | string) {
14-
// x is assigned in the if true branch, the type is not narrowed
1514
return typeof x === "string"
16-
? (x = 10 && x)// string | number
17-
: x; // string | number
15+
? ((x = "hello") && x) // string
16+
: x; // number
1817
}
1918
function foo3(x: number | string) {
20-
// x is assigned in the if false branch, the type is not narrowed
21-
// even though assigned using same type as narrowed expression
2219
return typeof x === "string"
23-
? (x = "Hello" && x) // string | number
24-
: x; // string | number
20+
? ((x = 10) && x) // number
21+
: x; // number
2522
}
2623
function foo4(x: number | string) {
27-
// false branch updates the variable - so here it is not number
28-
// even though assigned using same type as narrowed expression
2924
return typeof x === "string"
30-
? x // string | number
31-
: (x = 10 && x); // string | number
25+
? x // string
26+
: ((x = 10) && x); // number
3227
}
3328
function foo5(x: number | string) {
34-
// false branch updates the variable - so here it is not number
3529
return typeof x === "string"
36-
? x // string | number
37-
: (x = "hello" && x); // string | number
30+
? x // string
31+
: ((x = "hello") && x); // string
3832
}
3933
function foo6(x: number | string) {
4034
// Modify in both branches
4135
return typeof x === "string"
42-
? (x = 10 && x) // string | number
43-
: (x = "hello" && x); // string | number
36+
? ((x = 10) && x) // number
37+
: ((x = "hello") && x); // string
4438
}
4539
function foo7(x: number | string | boolean) {
4640
return typeof x === "string"
47-
? x === "hello" // string
41+
? x === "hello" // boolean
4842
: typeof x === "boolean"
4943
? x // boolean
50-
: x == 10; // number
44+
: x == 10; // boolean
5145
}
5246
function foo8(x: number | string | boolean) {
5347
var b: number | boolean;
@@ -56,14 +50,14 @@ function foo8(x: number | string | boolean) {
5650
: ((b = x) && // number | boolean
5751
(typeof x === "boolean"
5852
? x // boolean
59-
: x == 10)); // number
53+
: x == 10)); // boolean
6054
}
6155
function foo9(x: number | string) {
6256
var y = 10;
6357
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
6458
return typeof x === "string"
65-
? ((y = x.length) && x === "hello") // string
66-
: x === 10; // number
59+
? ((y = x.length) && x === "hello") // boolean
60+
: x === 10; // boolean
6761
}
6862
function foo10(x: number | string | boolean) {
6963
// Mixing typeguards
@@ -76,22 +70,20 @@ function foo10(x: number | string | boolean) {
7670
}
7771
function foo11(x: number | string | boolean) {
7872
// Mixing typeguards
79-
// Assigning value to x deep inside another guard stops narrowing of type too
8073
var b: number | boolean | string;
8174
return typeof x === "string"
82-
? x // number | boolean | string - changed in the false branch
83-
: ((b = x) // x is number | boolean | string - because the assignment changed it
75+
? x // string
76+
: ((b = x) // x is number | boolean
8477
&& typeof x === "number"
8578
&& (x = 10) // assignment to x
86-
&& x); // x is number | boolean | string
79+
&& x); // x is number
8780
}
8881
function foo12(x: number | string | boolean) {
8982
// Mixing typeguards
90-
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
9183
var b: number | boolean | string;
9284
return typeof x === "string"
93-
? (x = 10 && x.toString().length) // number | boolean | string - changed here
94-
: ((b = x) // x is number | boolean | string - changed in true branch
85+
? ((x = 10) && x.toString().length) // number
86+
: ((b = x) // x is number | boolean
9587
&& typeof x === "number"
9688
&& x); // x is number
9789
}

tests/cases/conformance/expressions/typeGuards/typeGuardsInIfStatement.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// In the true branch statement of an 'if' statement,
2-
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when true,
3-
// provided the true branch statement contains no assignments to the variable or parameter.
2+
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when true.
43
// In the false branch statement of an 'if' statement,
5-
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when false,
6-
// provided the false branch statement contains no assignments to the variable or parameter
4+
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when false.
75
function foo(x: number | string) {
86
if (typeof x === "string") {
97
return x.length; // string
@@ -13,54 +11,49 @@ function foo(x: number | string) {
1311
}
1412
}
1513
function foo2(x: number | string) {
16-
// x is assigned in the if true branch, the type is not narrowed
1714
if (typeof x === "string") {
1815
x = 10;
19-
return x; // string | number
16+
return x; // number
2017
}
2118
else {
22-
return x; // string | number
19+
return x; // number
2320
}
2421
}
2522
function foo3(x: number | string) {
26-
// x is assigned in the if true branch, the type is not narrowed
2723
if (typeof x === "string") {
28-
x = "Hello"; // even though assigned using same type as narrowed expression
29-
return x; // string | number
24+
x = "Hello";
25+
return x; // string
3026
}
3127
else {
32-
return x; // string | number
28+
return x; // number
3329
}
3430
}
3531
function foo4(x: number | string) {
36-
// false branch updates the variable - so here it is not number
3732
if (typeof x === "string") {
38-
return x; // string | number
33+
return x; // string
3934
}
4035
else {
41-
x = 10; // even though assigned number - this should result in x to be string | number
42-
return x; // string | number
36+
x = 10;
37+
return x; // number
4338
}
4439
}
4540
function foo5(x: number | string) {
46-
// false branch updates the variable - so here it is not number
4741
if (typeof x === "string") {
48-
return x; // string | number
42+
return x; // string
4943
}
5044
else {
5145
x = "hello";
52-
return x; // string | number
46+
return x; // string
5347
}
5448
}
5549
function foo6(x: number | string) {
56-
// Modify in both branches
5750
if (typeof x === "string") {
5851
x = 10;
59-
return x; // string | number
52+
return x; // number
6053
}
6154
else {
6255
x = "hello";
63-
return x; // string | number
56+
return x; // string
6457
}
6558
}
6659
function foo7(x: number | string | boolean) {

tests/cases/conformance/expressions/typeGuards/typeGuardsInRightOperandOfAndAndOperator.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// In the right operand of a && operation,
2-
// the type of a variable or parameter is narrowed by any type guard in the left operand when true,
3-
// provided the right operand contains no assignments to the variable or parameter.
2+
// the type of a variable or parameter is narrowed by any type guard in the left operand when true.
43
function foo(x: number | string) {
54
return typeof x === "string" && x.length === 10; // string
65
}
@@ -35,21 +34,11 @@ function foo7(x: number | string | boolean) {
3534
var y: number| boolean | string;
3635
var z: number| boolean | string;
3736
// Mixing typeguard narrowing
38-
// Assigning value to x deep inside another guard stops narrowing of type too
3937
return typeof x !== "string"
40-
&& ((z = x) // string | number | boolean - x changed deeper in conditional expression
38+
&& ((z = x) // number | boolean
4139
&& (typeof x === "number"
4240
// change value of x
43-
? (x = 10 && x.toString()) // number | boolean | string
41+
? ((x = 10) && x.toString()) // x is number
4442
// do not change value
45-
: (y = x && x.toString()))); // number | boolean | string
43+
: ((y = x) && x.toString()))); // x is boolean
4644
}
47-
function foo8(x: number | string) {
48-
// Mixing typeguard
49-
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
50-
return typeof x !== "string"
51-
&& (x = 10) // change x - number| string
52-
&& (typeof x === "number"
53-
? x // number
54-
: x.length); // string
55-
}

tests/cases/conformance/expressions/typeGuards/typeGuardsInRightOperandOfOrOrOperator.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,11 @@ function foo7(x: number | string | boolean) {
3535
var y: number| boolean | string;
3636
var z: number| boolean | string;
3737
// Mixing typeguard narrowing
38-
// Assigning value to x deep inside another guard stops narrowing of type too
3938
return typeof x === "string"
40-
|| ((z = x) // string | number | boolean - x changed deeper in conditional expression
39+
|| ((z = x) // number | boolean
4140
|| (typeof x === "number"
4241
// change value of x
43-
? (x = 10 && x.toString()) // number | boolean | string
42+
? ((x = 10) && x.toString()) // number | boolean | string
4443
// do not change value
45-
: (y = x && x.toString()))); // number | boolean | string
44+
: ((y = x) && x.toString()))); // number | boolean | string
4645
}
47-
function foo8(x: number | string) {
48-
// Mixing typeguard
49-
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
50-
return typeof x === "string"
51-
|| (x = 10) // change x - number| string
52-
|| (typeof x === "number"
53-
? x // number
54-
: x.length); // string
55-
}

tests/cases/conformance/types/typeRelationships/assignmentCompatibility/unionTypesAssignability.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ unionNumberString = null;
5959
unionDE = undefined;
6060
unionNumberString = undefined;
6161

62-
// type parameters
63-
function foo<T, U>(t: T, u: U) {
64-
t = u; // error
65-
u = t; // error
66-
var x : T | U;
67-
x = t; // ok
68-
x = u; // ok
69-
t = x; // error U not assignable to T
70-
u = x; // error T not assignable to U
62+
// type parameters
63+
function foo<T, U>(t: T, u: U) {
64+
t = u; // error
65+
u = t; // error
66+
var x : T | U;
67+
x = t; // ok
68+
x = u; // ok
69+
x = undefined;
70+
t = x; // error U not assignable to T
71+
u = x; // error T not assignable to U
7172
}

0 commit comments

Comments
 (0)