|
| 1 | +tests/cases/conformance/functions/functionImplementations.ts(90,1): error TS2838: This condition will always return 'false' since JavaScript compares objects by reference, not value. |
| 2 | + |
| 3 | + |
| 4 | +==== tests/cases/conformance/functions/functionImplementations.ts (1 errors) ==== |
| 5 | + // FunctionExpression with no return type annotation and no return statement returns void |
| 6 | + var v: void = function () { } (); |
| 7 | + |
| 8 | + // FunctionExpression f with no return type annotation and directly references f in its body returns any |
| 9 | + var a: any = function f() { |
| 10 | + return f; |
| 11 | + }; |
| 12 | + var a: any = function f() { |
| 13 | + return f(); |
| 14 | + }; |
| 15 | + |
| 16 | + // FunctionExpression f with no return type annotation and indirectly references f in its body returns any |
| 17 | + var a: any = function f() { |
| 18 | + var x = f; |
| 19 | + return x; |
| 20 | + }; |
| 21 | + |
| 22 | + // Two mutually recursive function implementations with no return type annotations |
| 23 | + function rec1() { |
| 24 | + return rec2(); |
| 25 | + } |
| 26 | + function rec2() { |
| 27 | + return rec1(); |
| 28 | + } |
| 29 | + var a = rec1(); |
| 30 | + var a = rec2(); |
| 31 | + |
| 32 | + // Two mutually recursive function implementations with return type annotation in one |
| 33 | + function rec3(): number { |
| 34 | + return rec4(); |
| 35 | + } |
| 36 | + function rec4() { |
| 37 | + return rec3(); |
| 38 | + } |
| 39 | + var n: number; |
| 40 | + var n = rec3(); |
| 41 | + var n = rec4(); |
| 42 | + |
| 43 | + // FunctionExpression with no return type annotation and returns a number |
| 44 | + var n = function () { |
| 45 | + return 3; |
| 46 | + } (); |
| 47 | + |
| 48 | + // FunctionExpression with no return type annotation and returns null |
| 49 | + var nu = null; |
| 50 | + var nu = function () { |
| 51 | + return null; |
| 52 | + } (); |
| 53 | + |
| 54 | + // FunctionExpression with no return type annotation and returns undefined |
| 55 | + var un = undefined; |
| 56 | + var un = function () { |
| 57 | + return undefined; |
| 58 | + } (); |
| 59 | + |
| 60 | + // FunctionExpression with no return type annotation and returns a type parameter type |
| 61 | + var n = function <T>(x: T) { |
| 62 | + return x; |
| 63 | + } (4); |
| 64 | + |
| 65 | + // FunctionExpression with no return type annotation and returns a constrained type parameter type |
| 66 | + var n = function <T extends {}>(x: T) { |
| 67 | + return x; |
| 68 | + } (4); |
| 69 | + |
| 70 | + // FunctionExpression with no return type annotation with multiple return statements with identical types |
| 71 | + var n = function () { |
| 72 | + return 3; |
| 73 | + return 5; |
| 74 | + }(); |
| 75 | + |
| 76 | + // Otherwise, the inferred return type is the first of the types of the return statement expressions |
| 77 | + // in the function body that is a supertype of each of the others, |
| 78 | + // ignoring return statements with no expressions. |
| 79 | + // A compile - time error occurs if no return statement expression has a type that is a supertype of each of the others. |
| 80 | + // FunctionExpression with no return type annotation with multiple return statements with subtype relation between returns |
| 81 | + class Base { private m; } |
| 82 | + class Derived extends Base { private q; } |
| 83 | + var b: Base; |
| 84 | + var b = function () { |
| 85 | + return new Base(); return new Derived(); |
| 86 | + } (); |
| 87 | + |
| 88 | + // FunctionExpression with no return type annotation with multiple return statements with one a recursive call |
| 89 | + var a = function f() { |
| 90 | + return new Base(); return new Derived(); return f(); // ? |
| 91 | + } (); |
| 92 | + |
| 93 | + // FunctionExpression with non -void return type annotation with a single throw statement |
| 94 | + undefined === function (): number { |
| 95 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 96 | + throw undefined; |
| 97 | + ~~~~~~~~~~~~~~~~~~~~ |
| 98 | + }; |
| 99 | + ~ |
| 100 | +!!! error TS2838: This condition will always return 'false' since JavaScript compares objects by reference, not value. |
| 101 | + |
| 102 | + // Type of 'this' in function implementation is 'any' |
| 103 | + function thisFunc() { |
| 104 | + var x = this; |
| 105 | + var x: any; |
| 106 | + } |
| 107 | + |
| 108 | + // Function signature with optional parameter, no type annotation and initializer has initializer's type |
| 109 | + function opt1(n = 4) { |
| 110 | + var m = n; |
| 111 | + var m: number; |
| 112 | + } |
| 113 | + |
| 114 | + // Function signature with optional parameter, no type annotation and initializer has initializer's widened type |
| 115 | + function opt2(n = { x: null, y: undefined }) { |
| 116 | + var m = n; |
| 117 | + var m: { x: any; y: any }; |
| 118 | + } |
| 119 | + |
| 120 | + // Function signature with initializer referencing other parameter to the left |
| 121 | + function opt3(n: number, m = n) { |
| 122 | + var y = m; |
| 123 | + var y: number; |
| 124 | + } |
| 125 | + |
| 126 | + // Function signature with optional parameter has correct codegen |
| 127 | + // (tested above) |
| 128 | + |
| 129 | + // FunctionExpression with non -void return type annotation return with no expression |
| 130 | + function f6(): number { |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + class Derived2 extends Base { private r: string; } |
| 135 | + class AnotherClass { private x } |
| 136 | + // if f is a contextually typed function expression, the inferred return type is the union type |
| 137 | + // of the types of the return statement expressions in the function body, |
| 138 | + // ignoring return statements with no expressions. |
| 139 | + var f7: (x: number) => string | number = x => { // should be (x: number) => number | string |
| 140 | + if (x < 0) { return x; } |
| 141 | + return x.toString(); |
| 142 | + } |
| 143 | + var f8: (x: number) => any = x => { // should be (x: number) => Base |
| 144 | + return new Base(); |
| 145 | + return new Derived2(); |
| 146 | + } |
| 147 | + var f9: (x: number) => any = x => { // should be (x: number) => Base |
| 148 | + return new Base(); |
| 149 | + return new Derived(); |
| 150 | + return new Derived2(); |
| 151 | + } |
| 152 | + var f10: (x: number) => any = x => { // should be (x: number) => Derived | Derived1 |
| 153 | + return new Derived(); |
| 154 | + return new Derived2(); |
| 155 | + } |
| 156 | + var f11: (x: number) => any = x => { // should be (x: number) => Base | AnotherClass |
| 157 | + return new Base(); |
| 158 | + return new AnotherClass(); |
| 159 | + } |
| 160 | + var f12: (x: number) => any = x => { // should be (x: number) => Base | AnotherClass |
| 161 | + return new Base(); |
| 162 | + return; // should be ignored |
| 163 | + return new AnotherClass(); |
| 164 | + } |
0 commit comments