Fixing some tests
This commit is contained in:
parent
560bc3f38c
commit
0820249e71
5 changed files with 51 additions and 86 deletions
|
@ -11,43 +11,37 @@ function foo(x: number | string) {
|
|||
: x++; // number
|
||||
}
|
||||
function foo2(x: number | string) {
|
||||
// x is assigned in the if true branch, the type is not narrowed
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x)// string | number
|
||||
: x; // string | number
|
||||
? ((x = "hello") && x) // string
|
||||
: x; // number
|
||||
}
|
||||
function foo3(x: number | string) {
|
||||
// x is assigned in the if false branch, the type is not narrowed
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string"
|
||||
? (x = "Hello" && x) // string | number
|
||||
: x; // string | number
|
||||
? ((x = 10) && x) // number
|
||||
: x; // number
|
||||
}
|
||||
function foo4(x: number | string) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
// even though assigned using same type as narrowed expression
|
||||
return typeof x === "string"
|
||||
? x // string | number
|
||||
: (x = 10 && x); // string | number
|
||||
? x // string
|
||||
: ((x = 10) && x); // number
|
||||
}
|
||||
function foo5(x: number | string) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
return typeof x === "string"
|
||||
? x // string | number
|
||||
: (x = "hello" && x); // string | number
|
||||
? x // string
|
||||
: ((x = "hello") && x); // string
|
||||
}
|
||||
function foo6(x: number | string) {
|
||||
// Modify in both branches
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x) // string | number
|
||||
: (x = "hello" && x); // string | number
|
||||
? ((x = 10) && x) // number
|
||||
: ((x = "hello") && x); // string
|
||||
}
|
||||
function foo7(x: number | string | boolean) {
|
||||
return typeof x === "string"
|
||||
? x === "hello" // string
|
||||
? x === "hello" // boolean
|
||||
: typeof x === "boolean"
|
||||
? x // boolean
|
||||
: x == 10; // number
|
||||
: x == 10; // boolean
|
||||
}
|
||||
function foo8(x: number | string | boolean) {
|
||||
var b: number | boolean;
|
||||
|
@ -56,14 +50,14 @@ function foo8(x: number | string | boolean) {
|
|||
: ((b = x) && // number | boolean
|
||||
(typeof x === "boolean"
|
||||
? x // boolean
|
||||
: x == 10)); // number
|
||||
: x == 10)); // boolean
|
||||
}
|
||||
function foo9(x: number | string) {
|
||||
var y = 10;
|
||||
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
|
||||
return typeof x === "string"
|
||||
? ((y = x.length) && x === "hello") // string
|
||||
: x === 10; // number
|
||||
? ((y = x.length) && x === "hello") // boolean
|
||||
: x === 10; // boolean
|
||||
}
|
||||
function foo10(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
|
@ -76,22 +70,20 @@ function foo10(x: number | string | boolean) {
|
|||
}
|
||||
function foo11(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
// Assigning value to x deep inside another guard stops narrowing of type too
|
||||
var b: number | boolean | string;
|
||||
return typeof x === "string"
|
||||
? x // number | boolean | string - changed in the false branch
|
||||
: ((b = x) // x is number | boolean | string - because the assignment changed it
|
||||
? x // string
|
||||
: ((b = x) // x is number | boolean
|
||||
&& typeof x === "number"
|
||||
&& (x = 10) // assignment to x
|
||||
&& x); // x is number | boolean | string
|
||||
&& x); // x is number
|
||||
}
|
||||
function foo12(x: number | string | boolean) {
|
||||
// Mixing typeguards
|
||||
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
|
||||
var b: number | boolean | string;
|
||||
return typeof x === "string"
|
||||
? (x = 10 && x.toString().length) // number | boolean | string - changed here
|
||||
: ((b = x) // x is number | boolean | string - changed in true branch
|
||||
? ((x = 10) && x.toString().length) // number
|
||||
: ((b = x) // x is number | boolean
|
||||
&& typeof x === "number"
|
||||
&& x); // x is number
|
||||
}
|
|
@ -1,9 +1,7 @@
|
|||
// In the true branch statement of an 'if' statement,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when true,
|
||||
// provided the true branch statement contains no assignments to the variable or parameter.
|
||||
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when true.
|
||||
// In the false branch statement of an 'if' statement,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when false,
|
||||
// provided the false branch statement contains no assignments to the variable or parameter
|
||||
// the type of a variable or parameter is narrowed by any type guard in the 'if' condition when false.
|
||||
function foo(x: number | string) {
|
||||
if (typeof x === "string") {
|
||||
return x.length; // string
|
||||
|
@ -13,54 +11,49 @@ function foo(x: number | string) {
|
|||
}
|
||||
}
|
||||
function foo2(x: number | string) {
|
||||
// x is assigned in the if true branch, the type is not narrowed
|
||||
if (typeof x === "string") {
|
||||
x = 10;
|
||||
return x; // string | number
|
||||
return x; // number
|
||||
}
|
||||
else {
|
||||
return x; // string | number
|
||||
return x; // number
|
||||
}
|
||||
}
|
||||
function foo3(x: number | string) {
|
||||
// x is assigned in the if true branch, the type is not narrowed
|
||||
if (typeof x === "string") {
|
||||
x = "Hello"; // even though assigned using same type as narrowed expression
|
||||
return x; // string | number
|
||||
x = "Hello";
|
||||
return x; // string
|
||||
}
|
||||
else {
|
||||
return x; // string | number
|
||||
return x; // number
|
||||
}
|
||||
}
|
||||
function foo4(x: number | string) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
if (typeof x === "string") {
|
||||
return x; // string | number
|
||||
return x; // string
|
||||
}
|
||||
else {
|
||||
x = 10; // even though assigned number - this should result in x to be string | number
|
||||
return x; // string | number
|
||||
x = 10;
|
||||
return x; // number
|
||||
}
|
||||
}
|
||||
function foo5(x: number | string) {
|
||||
// false branch updates the variable - so here it is not number
|
||||
if (typeof x === "string") {
|
||||
return x; // string | number
|
||||
return x; // string
|
||||
}
|
||||
else {
|
||||
x = "hello";
|
||||
return x; // string | number
|
||||
return x; // string
|
||||
}
|
||||
}
|
||||
function foo6(x: number | string) {
|
||||
// Modify in both branches
|
||||
if (typeof x === "string") {
|
||||
x = 10;
|
||||
return x; // string | number
|
||||
return x; // number
|
||||
}
|
||||
else {
|
||||
x = "hello";
|
||||
return x; // string | number
|
||||
return x; // string
|
||||
}
|
||||
}
|
||||
function foo7(x: number | string | boolean) {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
// In the right operand of a && operation,
|
||||
// the type of a variable or parameter is narrowed by any type guard in the left operand when true,
|
||||
// provided the right operand contains no assignments to the variable or parameter.
|
||||
// the type of a variable or parameter is narrowed by any type guard in the left operand when true.
|
||||
function foo(x: number | string) {
|
||||
return typeof x === "string" && x.length === 10; // string
|
||||
}
|
||||
|
@ -35,21 +34,11 @@ function foo7(x: number | string | boolean) {
|
|||
var y: number| boolean | string;
|
||||
var z: number| boolean | string;
|
||||
// Mixing typeguard narrowing
|
||||
// Assigning value to x deep inside another guard stops narrowing of type too
|
||||
return typeof x !== "string"
|
||||
&& ((z = x) // string | number | boolean - x changed deeper in conditional expression
|
||||
&& ((z = x) // number | boolean
|
||||
&& (typeof x === "number"
|
||||
// change value of x
|
||||
? (x = 10 && x.toString()) // number | boolean | string
|
||||
? ((x = 10) && x.toString()) // x is number
|
||||
// do not change value
|
||||
: (y = x && x.toString()))); // number | boolean | string
|
||||
: ((y = x) && x.toString()))); // x is boolean
|
||||
}
|
||||
function foo8(x: number | string) {
|
||||
// Mixing typeguard
|
||||
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
|
||||
return typeof x !== "string"
|
||||
&& (x = 10) // change x - number| string
|
||||
&& (typeof x === "number"
|
||||
? x // number
|
||||
: x.length); // string
|
||||
}
|
|
@ -35,21 +35,11 @@ function foo7(x: number | string | boolean) {
|
|||
var y: number| boolean | string;
|
||||
var z: number| boolean | string;
|
||||
// Mixing typeguard narrowing
|
||||
// Assigning value to x deep inside another guard stops narrowing of type too
|
||||
return typeof x === "string"
|
||||
|| ((z = x) // string | number | boolean - x changed deeper in conditional expression
|
||||
|| ((z = x) // number | boolean
|
||||
|| (typeof x === "number"
|
||||
// change value of x
|
||||
? (x = 10 && x.toString()) // number | boolean | string
|
||||
? ((x = 10) && x.toString()) // number | boolean | string
|
||||
// do not change value
|
||||
: (y = x && x.toString()))); // number | boolean | string
|
||||
: ((y = x) && x.toString()))); // number | boolean | string
|
||||
}
|
||||
function foo8(x: number | string) {
|
||||
// Mixing typeguard
|
||||
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
|
||||
return typeof x === "string"
|
||||
|| (x = 10) // change x - number| string
|
||||
|| (typeof x === "number"
|
||||
? x // number
|
||||
: x.length); // string
|
||||
}
|
|
@ -59,13 +59,14 @@ unionNumberString = null;
|
|||
unionDE = undefined;
|
||||
unionNumberString = undefined;
|
||||
|
||||
// type parameters
|
||||
function foo<T, U>(t: T, u: U) {
|
||||
t = u; // error
|
||||
u = t; // error
|
||||
var x : T | U;
|
||||
x = t; // ok
|
||||
x = u; // ok
|
||||
t = x; // error U not assignable to T
|
||||
u = x; // error T not assignable to U
|
||||
// type parameters
|
||||
function foo<T, U>(t: T, u: U) {
|
||||
t = u; // error
|
||||
u = t; // error
|
||||
var x : T | U;
|
||||
x = t; // ok
|
||||
x = u; // ok
|
||||
x = undefined;
|
||||
t = x; // error U not assignable to T
|
||||
u = x; // error T not assignable to U
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue