File: typeofThis.js

package info (click to toggle)
node-typescript 5.0.4%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 459,116 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (282 lines) | stat: -rw-r--r-- 5,757 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//// [typeofThis.ts]
class Test {
    data = {};
    constructor() {
        var copy: typeof this.data = {};
    }
}

class Test1 {
    data = { foo: '' };
    ['this'] = '';
    constructor() {
        var copy: typeof this.data = { foo: '' };
        var foo: typeof this.data.foo = '';

        var self: typeof this = this;
        self.data;

        var str: typeof this.this = '';
    }
}


function Test2() {
    let x: typeof this.no = 1;
}

function Test3(this: { no: number }) {
    let x: typeof this.no = 1;
}

function Test4(this: { no: number } | undefined) {
    let x: typeof this.no = 1;
}

class Test5 {
    no = 1;

    f = () => {
        // should not capture this.
        let x: typeof this.no = 1;
    }
}

namespace Test6 {
    export let f = () => {
        let x: typeof this.no = 1;
    }
}

module Test7 {
    export let f = () => {
        let x: typeof this.no = 1;
    }
}

const Test8 = () => {
    let x: typeof this.no = 1;
}

class Test9 {
    no = 0;
    this = 0;

    f() {
        if (this instanceof Test9D1) {
            const d1: typeof this = this;
            d1.f1();
        }

        if (this instanceof Test9D2) {
            const d2: typeof this = this;
            d2.f2();
        }
    }

    g() {
        if (this.no === 1) {
            const no: typeof this.no = this.no;
        }

        if (this.this === 1) {
            const no: typeof this.this = this.this;
        }
    }
}

class Test9D1 {
    f1() {}
}

class Test9D2 {
    f2() {}
}

class Test10 {
    a?: { b?: string }

    foo() {
        let a: typeof this.a = undefined as any;
        if (this.a) {
            let a: typeof this.a = undefined as any;    // should narrow to { b?: string }
            let b: typeof this.a.b = undefined as any;

            if (this.a.b) {
                let b: typeof this.a.b = undefined as any;   // should narrow to string
            }
        }
    }
}

class Test11 {
    this?: { x?: string };
    
    foo() {
        const o = this;
        let bar: typeof o.this = {};

        if (o.this && o.this.x) {
            let y: string = o.this.x;   // should narrow to string
        }
    }
}

class Tests12 {
    test1() { // OK
        type Test = typeof this;
    }

    test2() { // OK
        for (;;) {}
        type Test = typeof this;
    }

    test3() { // expected no compile errors
        for (const dummy in []) {}
        type Test = typeof this;
    }

    test4() { // expected no compile errors
        for (const dummy of []) {}
        type Test = typeof this;
    }
}

//// [typeofThis.js]
"use strict";
var Test = /** @class */ (function () {
    function Test() {
        this.data = {};
        var copy = {};
    }
    return Test;
}());
var Test1 = /** @class */ (function () {
    function Test1() {
        this.data = { foo: '' };
        this['this'] = '';
        var copy = { foo: '' };
        var foo = '';
        var self = this;
        self.data;
        var str = '';
    }
    return Test1;
}());
function Test2() {
    var x = 1;
}
function Test3() {
    var x = 1;
}
function Test4() {
    var x = 1;
}
var Test5 = /** @class */ (function () {
    function Test5() {
        this.no = 1;
        this.f = function () {
            // should not capture this.
            var x = 1;
        };
    }
    return Test5;
}());
var Test6;
(function (Test6) {
    Test6.f = function () {
        var x = 1;
    };
})(Test6 || (Test6 = {}));
var Test7;
(function (Test7) {
    Test7.f = function () {
        var x = 1;
    };
})(Test7 || (Test7 = {}));
var Test8 = function () {
    var x = 1;
};
var Test9 = /** @class */ (function () {
    function Test9() {
        this.no = 0;
        this.this = 0;
    }
    Test9.prototype.f = function () {
        if (this instanceof Test9D1) {
            var d1 = this;
            d1.f1();
        }
        if (this instanceof Test9D2) {
            var d2 = this;
            d2.f2();
        }
    };
    Test9.prototype.g = function () {
        if (this.no === 1) {
            var no = this.no;
        }
        if (this.this === 1) {
            var no = this.this;
        }
    };
    return Test9;
}());
var Test9D1 = /** @class */ (function () {
    function Test9D1() {
    }
    Test9D1.prototype.f1 = function () { };
    return Test9D1;
}());
var Test9D2 = /** @class */ (function () {
    function Test9D2() {
    }
    Test9D2.prototype.f2 = function () { };
    return Test9D2;
}());
var Test10 = /** @class */ (function () {
    function Test10() {
    }
    Test10.prototype.foo = function () {
        var a = undefined;
        if (this.a) {
            var a_1 = undefined; // should narrow to { b?: string }
            var b = undefined;
            if (this.a.b) {
                var b_1 = undefined; // should narrow to string
            }
        }
    };
    return Test10;
}());
var Test11 = /** @class */ (function () {
    function Test11() {
    }
    Test11.prototype.foo = function () {
        var o = this;
        var bar = {};
        if (o.this && o.this.x) {
            var y = o.this.x; // should narrow to string
        }
    };
    return Test11;
}());
var Tests12 = /** @class */ (function () {
    function Tests12() {
    }
    Tests12.prototype.test1 = function () {
    };
    Tests12.prototype.test2 = function () {
        for (;;) { }
    };
    Tests12.prototype.test3 = function () {
        for (var dummy in []) { }
    };
    Tests12.prototype.test4 = function () {
        for (var _i = 0, _a = []; _i < _a.length; _i++) {
            var dummy = _a[_i];
        }
    };
    return Tests12;
}());