|
| 1 | +thislessFunctionsNotContextSensitive2.ts(52,14): error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. |
| 2 | +thislessFunctionsNotContextSensitive2.ts(83,12): error TS7041: The containing arrow function captures the global value of 'this'. |
| 3 | + |
| 4 | + |
| 5 | +==== thislessFunctionsNotContextSensitive2.ts (2 errors) ==== |
| 6 | + interface Options<Context, Data> { |
| 7 | + context: Context; |
| 8 | + produce(this: Context): Data; |
| 9 | + consume(this: Context, data: Data): void; |
| 10 | + } |
| 11 | + |
| 12 | + declare function defineOptions<Context, Data>( |
| 13 | + options: Options<Context, Data>, |
| 14 | + ): [Context, Data]; |
| 15 | + |
| 16 | + const result1 = defineOptions({ |
| 17 | + context: { tag: "A", value: 1 }, |
| 18 | + consume(_data) {}, |
| 19 | + produce() { |
| 20 | + return 42; |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + const result2 = defineOptions({ |
| 25 | + context: { tag: "B", value: 2 }, |
| 26 | + consume(_data) {}, |
| 27 | + produce() { |
| 28 | + return this.value; |
| 29 | + }, |
| 30 | + }); |
| 31 | + |
| 32 | + const result3 = defineOptions({ |
| 33 | + context: { tag: "C", value: 3 }, |
| 34 | + consume(_data) {}, |
| 35 | + produce: () => 123, |
| 36 | + }); |
| 37 | + |
| 38 | + const result4 = defineOptions({ |
| 39 | + context: { tag: "D", value: 4 }, |
| 40 | + consume(_data) {}, |
| 41 | + produce() { |
| 42 | + class Local { |
| 43 | + value = 'foo'; |
| 44 | + get() { |
| 45 | + return this.value; |
| 46 | + } |
| 47 | + } |
| 48 | + return new Local().get();; |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + const result5 = defineOptions({ |
| 53 | + context: { tag: "E", value: 5 }, |
| 54 | + consume(_data) {}, |
| 55 | + produce() { |
| 56 | + function inner() { |
| 57 | + return this; |
| 58 | + ~~~~ |
| 59 | +!!! error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation. |
| 60 | +!!! related TS2738 thislessFunctionsNotContextSensitive2.ts:51:14: An outer value of 'this' is shadowed by this container. |
| 61 | + } |
| 62 | + return inner(); |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + const result6 = defineOptions({ |
| 67 | + context: { tag: "F", value: 6 }, |
| 68 | + consume(_data) {}, |
| 69 | + produce() { |
| 70 | + const arrow = () => this.value; |
| 71 | + return arrow(); |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + const result7 = defineOptions({ |
| 76 | + context: { tag: "G", value: 7 }, |
| 77 | + consume(_data) {}, |
| 78 | + produce() { |
| 79 | + const self = this; |
| 80 | + function inner() { |
| 81 | + return self.value; |
| 82 | + } |
| 83 | + return inner(); |
| 84 | + }, |
| 85 | + }); |
| 86 | + |
| 87 | + const result8 = defineOptions({ |
| 88 | + context: { tag: "H", value: 8 }, |
| 89 | + consume(_data) {}, |
| 90 | + produce: () => { |
| 91 | + return this; |
| 92 | + ~~~~ |
| 93 | +!!! error TS7041: The containing arrow function captures the global value of 'this'. |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + const result9 = defineOptions({ |
| 98 | + context: { tag: "I", value: 9 }, |
| 99 | + consume(_data) {}, |
| 100 | + produce() { |
| 101 | + const obj = { |
| 102 | + value: 'foo', |
| 103 | + get() { |
| 104 | + return this.value; |
| 105 | + }, |
| 106 | + }; |
| 107 | + return obj.get(); |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + const result10 = defineOptions({ |
| 112 | + context: { tag: "I", value: 9 }, |
| 113 | + consume(_data) {}, |
| 114 | + produce() { |
| 115 | + interface Foo { |
| 116 | + prop: this; |
| 117 | + } |
| 118 | + return {} as Foo; |
| 119 | + }, |
| 120 | + }); |
| 121 | + |
| 122 | + const result11 = defineOptions({ |
| 123 | + context: { tag: "I", value: 9 }, |
| 124 | + consume(_data) {}, |
| 125 | + produce() { |
| 126 | + function fn(this: { prop: string }) { |
| 127 | + return this.prop; |
| 128 | + } |
| 129 | + return fn; |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
0 commit comments