|
| 1 | +tests/cases/conformance/expressions/typeGuards/typeGuardsInConditionalExpression.ts(93,22): error TS2349: Cannot invoke an expression whose type lacks a call signature. |
| 2 | + |
| 3 | + |
| 4 | +==== tests/cases/conformance/expressions/typeGuards/typeGuardsInConditionalExpression.ts (1 errors) ==== |
| 5 | + // In the true expression of a conditional expression, |
| 6 | + // the type of a variable or parameter is narrowed by any type guard in the condition when true, |
| 7 | + // provided the true expression contains no assignments to the variable or parameter. |
| 8 | + // In the false expression of a conditional expression, |
| 9 | + // the type of a variable or parameter is narrowed by any type guard in the condition when false, |
| 10 | + // provided the false expression contains no assignments to the variable or parameter. |
| 11 | + |
| 12 | + function foo(x: number | string) { |
| 13 | + return typeof x === "string" |
| 14 | + ? x.length // string |
| 15 | + : x++; // number |
| 16 | + } |
| 17 | + function foo2(x: number | string) { |
| 18 | + // x is assigned in the if true branch, the type is not narrowed |
| 19 | + return typeof x === "string" |
| 20 | + ? (x = 10 && x)// string | number |
| 21 | + : x; // string | number |
| 22 | + } |
| 23 | + function foo3(x: number | string) { |
| 24 | + // x is assigned in the if false branch, the type is not narrowed |
| 25 | + // even though assigned using same type as narrowed expression |
| 26 | + return typeof x === "string" |
| 27 | + ? (x = "Hello" && x) // string | number |
| 28 | + : x; // string | number |
| 29 | + } |
| 30 | + function foo4(x: number | string) { |
| 31 | + // false branch updates the variable - so here it is not number |
| 32 | + // even though assigned using same type as narrowed expression |
| 33 | + return typeof x === "string" |
| 34 | + ? x // string | number |
| 35 | + : (x = 10 && x); // string | number |
| 36 | + } |
| 37 | + function foo5(x: number | string) { |
| 38 | + // false branch updates the variable - so here it is not number |
| 39 | + return typeof x === "string" |
| 40 | + ? x // string | number |
| 41 | + : (x = "hello" && x); // string | number |
| 42 | + } |
| 43 | + function foo6(x: number | string) { |
| 44 | + // Modify in both branches |
| 45 | + return typeof x === "string" |
| 46 | + ? (x = 10 && x) // string | number |
| 47 | + : (x = "hello" && x); // string | number |
| 48 | + } |
| 49 | + function foo7(x: number | string | boolean) { |
| 50 | + return typeof x === "string" |
| 51 | + ? x === "hello" // string |
| 52 | + : typeof x === "boolean" |
| 53 | + ? x // boolean |
| 54 | + : x == 10; // number |
| 55 | + } |
| 56 | + function foo8(x: number | string | boolean) { |
| 57 | + var b: number | boolean; |
| 58 | + return typeof x === "string" |
| 59 | + ? x === "hello" |
| 60 | + : ((b = x) && // number | boolean |
| 61 | + (typeof x === "boolean" |
| 62 | + ? x // boolean |
| 63 | + : x == 10)); // number |
| 64 | + } |
| 65 | + function foo9(x: number | string) { |
| 66 | + var y = 10; |
| 67 | + // usage of x or assignment to separate variable shouldn't cause narrowing of type to stop |
| 68 | + return typeof x === "string" |
| 69 | + ? ((y = x.length) && x === "hello") // string |
| 70 | + : x === 10; // number |
| 71 | + } |
| 72 | + function foo10(x: number | string | boolean) { |
| 73 | + // Mixing typeguards |
| 74 | + var b: boolean | number; |
| 75 | + return typeof x === "string" |
| 76 | + ? x // string |
| 77 | + : ((b = x) // x is number | boolean |
| 78 | + && typeof x === "number" |
| 79 | + && x.toString()); // x is number |
| 80 | + } |
| 81 | + function foo11(x: number | string | boolean) { |
| 82 | + // Mixing typeguards |
| 83 | + // Assigning value to x deep inside another guard stops narrowing of type too |
| 84 | + var b: number | boolean | string; |
| 85 | + return typeof x === "string" |
| 86 | + ? x // number | boolean | string - changed in the false branch |
| 87 | + : ((b = x) // x is number | boolean | string - because the assignment changed it |
| 88 | + && typeof x === "number" |
| 89 | + && (x = 10) // assignment to x |
| 90 | + && x); // x is number | boolean | string |
| 91 | + } |
| 92 | + function foo12(x: number | string | boolean) { |
| 93 | + // Mixing typeguards |
| 94 | + // Assigning value to x in outer guard shouldn't stop narrowing in the inner expression |
| 95 | + var b: number | boolean | string; |
| 96 | + return typeof x === "string" |
| 97 | + ? (x = 10 && x.toString().length) // number | boolean | string - changed here |
| 98 | + ~~~~~~~~~~~~ |
| 99 | +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. |
| 100 | + : ((b = x) // x is number | boolean | string - changed in true branch |
| 101 | + && typeof x === "number" |
| 102 | + && x); // x is number |
| 103 | + } |
0 commit comments