File: thisTypeInFunctions.errors.txt

package info (click to toggle)
node-typescript 4.9.5%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 533,908 kB
  • sloc: javascript: 2,018,330; makefile: 7; sh: 1
file content (206 lines) | stat: -rw-r--r-- 7,988 bytes parent folder | download | duplicates (4)
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
tests/cases/conformance/types/thisType/thisTypeInFunctions.ts(118,29): error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
tests/cases/conformance/types/thisType/thisTypeInFunctions.ts(119,32): error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
tests/cases/conformance/types/thisType/thisTypeInFunctions.ts(120,36): error TS2339: Property 'n' does not exist on type 'typeof globalThis'.


==== tests/cases/conformance/types/thisType/thisTypeInFunctions.ts (3 errors) ====
    // body checking
    class B {
        n: number;
    }
    class C {
        n: number;
        explicitThis(this: this, m: number): number {
            return this.n + m;
        }
        explicitC(this: C, m: number): number {
            return this.n + m;
        }
        explicitProperty(this: {n: number}, m: number): number {
            return this.n + m;
        }
        explicitVoid(this: void, m: number): number {
            return m + 1;
        }
    }
    class D extends C { }
    interface I {
        a: number;
        explicitVoid1(this: void): number;
        explicitVoid2(this: void): number;
        explicitStructural(this: {a: number}): number;
        explicitInterface(this: I): number;
        explicitThis(this: this): number;
    }
    function explicitStructural(this: { y: number }, x: number): number {
        return x + this.y;
    }
    function justThis(this: { y: number }): number {
        return this.y;
    }
    function implicitThis(n: number): number {
        return this.m + n + 12;
    }
    let impl: I = {
        a: 12,
        explicitVoid2: () => this.a, // ok, this: any because it refers to some outer object (window?)
        explicitVoid1() { return 12; },
        explicitStructural() {
            return this.a;
        },
        explicitInterface() {
            return this.a;
        },
        explicitThis() {
            return this.a;
        },
    }
    impl.explicitVoid1 = function () { return 12; };
    impl.explicitVoid2 = () => 12;
    impl.explicitStructural = function() { return this.a; };
    impl.explicitInterface = function() { return this.a; };
    impl.explicitStructural = () => 12;
    impl.explicitInterface = () => 12;
    impl.explicitThis = function () { return this.a; };
    // parameter checking
    let ok: {y: number, f: (this: { y: number }, x: number) => number} = { y: 12, f: explicitStructural };
    let implicitAnyOk: {notSpecified: number, f: (x: number) => number} = { notSpecified: 12, f: implicitThis };
    ok.f(13);
    implicitThis(12);
    implicitAnyOk.f(12);
    
    let c = new C();
    let d = new D();
    let ripped = c.explicitC;
    c.explicitC(12);
    c.explicitProperty(12);
    c.explicitThis(12);
    d.explicitC(12);
    d.explicitProperty(12);
    d.explicitThis(12);
    let reconstructed: { 
        n: number,
        explicitThis(this: C, m: number): number, // note: this: this is not allowed in an object literal type.
        explicitC(this: C, m: number): number,
        explicitProperty: (this: {n : number}, m: number) => number,
        explicitVoid(this: void, m: number): number,
    } = { 
        n: 12,
        explicitThis: c.explicitThis,
        explicitC: c.explicitC,
        explicitProperty: c.explicitProperty,
        explicitVoid: c.explicitVoid
    };
    reconstructed.explicitThis(10);
    reconstructed.explicitProperty(11);
    let explicitVoid = reconstructed.explicitVoid;
    explicitVoid(12);
    // assignment checking
    let unboundToSpecified: (this: { y: number }, x: number) => number = x => x + this.y; // ok, this:any
    let specifiedToSpecified: (this: {y: number}, x: number) => number = explicitStructural;
    let anyToSpecified: (this: { y: number }, x: number) => number = function(x: number): number { return x + 12; };
    
    let unspecifiedLambda: (x: number) => number = x => x + 12;
    let specifiedLambda: (this: void, x: number) => number = x => x + 12;
    let unspecifiedLambdaToSpecified: (this: {y: number}, x: number) => number = unspecifiedLambda;
    let specifiedLambdaToSpecified: (this: {y: number}, x: number) => number = specifiedLambda;
    
    
    let explicitCFunction: (this: C, m: number) => number;
    let explicitPropertyFunction: (this: {n: number}, m: number) => number;
    c.explicitC = explicitCFunction;
    c.explicitC = function(this: C, m: number) { return this.n + m };
    c.explicitProperty = explicitPropertyFunction;
    c.explicitProperty = function(this: {n: number}, m: number) { return this.n + m };
    c.explicitProperty = reconstructed.explicitProperty;
    
    // lambdas are assignable to anything
    c.explicitC = m => m;
    c.explicitThis = m => m;
    c.explicitProperty = m => m;
    
    // this inside lambdas refer to outer scope
    // the outer-scoped lambda at top-level is still just `any`
    c.explicitC = m => m + this.n;
                                ~
!!! error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
    c.explicitThis = m => m + this.n;
                                   ~
!!! error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
    c.explicitProperty = m => m + this.n;
                                       ~
!!! error TS2339: Property 'n' does not exist on type 'typeof globalThis'.
    
    //NOTE: this=C here, I guess?
    c.explicitThis = explicitCFunction;
    c.explicitThis = function(this: C, m: number) { return this.n + m };
    
    // this:any compatibility
    c.explicitC = function(m) { return this.n + m };
    c.explicitProperty = function(m) { return this.n + m };
    c.explicitThis = function(m) { return this.n + m };
    
    // this: contextual typing
    c.explicitThis = function(this, m) { return this.n + m };
    
    // this: superclass compatibility
    c.explicitC = function(this: B, m: number) { return this.n + m };
    
    // this:void compatibility
    c.explicitVoid = n => n;
    
    // class-based assignability
    class Base1 {
        x: number;
        public polymorphic(this: this): number { return this.x; }
        explicit(this: Base1): number { return this.x; }
        static explicitStatic(this: typeof Base1): number { return this.y; }
        static y: number;
    }
    class Derived1 extends Base1 {
        y: number
    }
    class Base2 {
        y: number
        polymorphic(this: this): number { return this.y; }
        explicit(this: Base1): number { return this.x; }
    }
    class Derived2 extends Base2 {
        x: number
    }
    let b1 = new Base1();
    let b2 = new Base2();
    let d1 = new Derived1();
    let d2 = new Derived2();
    d2.polymorphic = d1.polymorphic // ok, 'x' and 'y' in { x, y }
    d1.polymorphic = d2.polymorphic // ok, 'x' and 'y' in { x, y }
    
    // bivariance-allowed cases
    d1.polymorphic = b2.polymorphic // ok, 'y' in D: { x, y }
    d2.polymorphic = d1.explicit // ok, 'y' in { x, y }
    b1.polymorphic = d2.polymorphic // ok, 'x' and 'y' not in Base1: { x }
    b1.explicit = d2.polymorphic // ok, 'x' and 'y' not in Base1: { x }
    
    ////// use this-type for construction with new ////
    function InterfaceThis(this: I) {
        this.a = 12;
    }
    function LiteralTypeThis(this: {x: string}) {
        this.x = "ok";
    }
    function AnyThis(this: any) {
        this.x = "ok";
    }
    let interfaceThis = new InterfaceThis();
    let literalTypeThis = new LiteralTypeThis();
    let anyThis = new AnyThis();
    
    //// type parameter inference ////
    declare var f: { 
        (this: void, x: number): number, 
        call<U>(this: (...argArray: any[]) => U, ...argArray: any[]): U;
    };
    let n: number = f.call(12);
    
    function missingTypeIsImplicitAny(this, a: number) { return this.anything + a; }