Skip to content

Commit 5bda48b

Browse files
committed
Add tests
1 parent cd87d90 commit 5bda48b

4 files changed

Lines changed: 1310 additions & 0 deletions

File tree

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
//// [thisTypeInObjectLiterals2.ts]
2+
3+
// In methods of an object literal with no contextual type, 'this' has the type
4+
// of the object literal.
5+
6+
let obj1 = {
7+
a: 1,
8+
f() {
9+
return this.a;
10+
},
11+
b: "hello",
12+
c: {
13+
g() {
14+
this.g();
15+
}
16+
},
17+
get d() {
18+
return this.a;
19+
},
20+
get e() {
21+
return this.b;
22+
},
23+
set e(value) {
24+
this.b = value;
25+
}
26+
};
27+
28+
// In methods of an object literal with a contextual type, 'this' has the
29+
// contextual type.
30+
31+
type Point = {
32+
x: number;
33+
y: number;
34+
moveBy(dx: number, dy: number): void;
35+
}
36+
37+
let p1: Point = {
38+
x: 10,
39+
y: 20,
40+
moveBy(dx, dy) {
41+
this.x += dx;
42+
this.y += dy;
43+
}
44+
};
45+
46+
declare function f1(p: Point): void;
47+
48+
f1({
49+
x: 10,
50+
y: 20,
51+
moveBy(dx, dy) {
52+
this.x += dx;
53+
this.y += dy;
54+
}
55+
});
56+
57+
// In methods of an object literal with a contextual type that includes some
58+
// ThisType<T>, 'this' is of type T.
59+
60+
type ObjectDescriptor<D, M> = {
61+
data?: D;
62+
methods?: M & ThisType<D & M>; // Type of 'this' in methods is D & M
63+
}
64+
65+
declare function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M;
66+
67+
let x1 = makeObject({
68+
data: { x: 0, y: 0 },
69+
methods: {
70+
moveBy(dx: number, dy: number) {
71+
this.x += dx; // Strongly typed this
72+
this.y += dy; // Strongly typed this
73+
}
74+
}
75+
});
76+
77+
// In methods contained in an object literal with a contextual type that includes
78+
// some ThisType<T>, 'this' is of type T.
79+
80+
type ObjectDescriptor2<D, M> = ThisType<D & M> & {
81+
data?: D;
82+
methods?: M;
83+
}
84+
85+
declare function makeObject2<D, M>(desc: ObjectDescriptor<D, M>): D & M;
86+
87+
let x2 = makeObject2({
88+
data: { x: 0, y: 0 },
89+
methods: {
90+
moveBy(dx: number, dy: number) {
91+
this.x += dx; // Strongly typed this
92+
this.y += dy; // Strongly typed this
93+
}
94+
}
95+
});
96+
97+
// Proof of concept for typing of Vue.js
98+
99+
type Accessors<T> = { [K in keyof T]: (() => T[K]) | Computed<T[K]> };
100+
101+
type Dictionary<T> = { [x: string]: T }
102+
103+
type Computed<T> = {
104+
get?(): T;
105+
set?(value: T): void;
106+
}
107+
108+
type VueOptions<D, M, P> = ThisType<D & M & P> & {
109+
data?: D | (() => D);
110+
methods?: M;
111+
computed?: Accessors<P>;
112+
}
113+
114+
declare const Vue: new <D, M, P>(options: VueOptions<D, M, P>) => D & M & P;
115+
116+
let vue = new Vue({
117+
data: () => ({ x: 1, y: 2 }),
118+
methods: {
119+
f(x: string) {
120+
return this.x;
121+
}
122+
},
123+
computed: {
124+
test(): number {
125+
return this.x;
126+
},
127+
hello: {
128+
get() {
129+
return "hi";
130+
},
131+
set(value: string) {
132+
}
133+
}
134+
}
135+
});
136+
137+
vue;
138+
vue.x;
139+
vue.f("abc");
140+
vue.test;
141+
vue.hello;
142+
143+
144+
//// [thisTypeInObjectLiterals2.js]
145+
// In methods of an object literal with no contextual type, 'this' has the type
146+
// of the object literal.
147+
var obj1 = {
148+
a: 1,
149+
f: function () {
150+
return this.a;
151+
},
152+
b: "hello",
153+
c: {
154+
g: function () {
155+
this.g();
156+
}
157+
},
158+
get d() {
159+
return this.a;
160+
},
161+
get e() {
162+
return this.b;
163+
},
164+
set e(value) {
165+
this.b = value;
166+
}
167+
};
168+
var p1 = {
169+
x: 10,
170+
y: 20,
171+
moveBy: function (dx, dy) {
172+
this.x += dx;
173+
this.y += dy;
174+
}
175+
};
176+
f1({
177+
x: 10,
178+
y: 20,
179+
moveBy: function (dx, dy) {
180+
this.x += dx;
181+
this.y += dy;
182+
}
183+
});
184+
var x1 = makeObject({
185+
data: { x: 0, y: 0 },
186+
methods: {
187+
moveBy: function (dx, dy) {
188+
this.x += dx; // Strongly typed this
189+
this.y += dy; // Strongly typed this
190+
}
191+
}
192+
});
193+
var x2 = makeObject2({
194+
data: { x: 0, y: 0 },
195+
methods: {
196+
moveBy: function (dx, dy) {
197+
this.x += dx; // Strongly typed this
198+
this.y += dy; // Strongly typed this
199+
}
200+
}
201+
});
202+
var vue = new Vue({
203+
data: function () { return ({ x: 1, y: 2 }); },
204+
methods: {
205+
f: function (x) {
206+
return this.x;
207+
}
208+
},
209+
computed: {
210+
test: function () {
211+
return this.x;
212+
},
213+
hello: {
214+
get: function () {
215+
return "hi";
216+
},
217+
set: function (value) {
218+
}
219+
}
220+
}
221+
});
222+
vue;
223+
vue.x;
224+
vue.f("abc");
225+
vue.test;
226+
vue.hello;
227+
228+
229+
//// [thisTypeInObjectLiterals2.d.ts]
230+
declare let obj1: {
231+
a: number;
232+
f(): number;
233+
b: string;
234+
c: {
235+
g(): void;
236+
};
237+
readonly d: number;
238+
e: string;
239+
};
240+
declare type Point = {
241+
x: number;
242+
y: number;
243+
moveBy(dx: number, dy: number): void;
244+
};
245+
declare let p1: Point;
246+
declare function f1(p: Point): void;
247+
declare type ObjectDescriptor<D, M> = {
248+
data?: D;
249+
methods?: M & ThisType<D & M>;
250+
};
251+
declare function makeObject<D, M>(desc: ObjectDescriptor<D, M>): D & M;
252+
declare let x1: {
253+
x: number;
254+
y: number;
255+
} & {
256+
moveBy(dx: number, dy: number): void;
257+
};
258+
declare type ObjectDescriptor2<D, M> = ThisType<D & M> & {
259+
data?: D;
260+
methods?: M;
261+
};
262+
declare function makeObject2<D, M>(desc: ObjectDescriptor<D, M>): D & M;
263+
declare let x2: {
264+
x: number;
265+
y: number;
266+
} & {
267+
moveBy(dx: number, dy: number): void;
268+
};
269+
declare type Accessors<T> = {
270+
[K in keyof T]: (() => T[K]) | Computed<T[K]>;
271+
};
272+
declare type Dictionary<T> = {
273+
[x: string]: T;
274+
};
275+
declare type Computed<T> = {
276+
get?(): T;
277+
set?(value: T): void;
278+
};
279+
declare type VueOptions<D, M, P> = ThisType<D & M & P> & {
280+
data?: D | (() => D);
281+
methods?: M;
282+
computed?: Accessors<P>;
283+
};
284+
declare const Vue: new <D, M, P>(options: VueOptions<D, M, P>) => D & M & P;
285+
declare let vue: {
286+
x: number;
287+
y: number;
288+
} & {
289+
f(x: string): number;
290+
} & {
291+
test: number;
292+
hello: string;
293+
};

0 commit comments

Comments
 (0)