forked from microsoft/TypeScript
/
asyncFunctionReturnType.js
175 lines (156 loc) · 6.19 KB
/
asyncFunctionReturnType.js
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
//// [asyncFunctionReturnType.ts]
async function fAsync() {
// Without explicit type annotation, this is just an array.
return [1, true];
}
async function fAsyncExplicit(): Promise<[number, boolean]> {
// This is contextually typed as a tuple.
return [1, true];
}
// https://github.com/Microsoft/TypeScript/issues/13128
interface Obj {
stringProp: string;
anyProp: any;
}
async function fIndexedTypeForStringProp(obj: Obj): Promise<Obj["stringProp"]> {
return obj.stringProp;
}
async function fIndexedTypeForPromiseOfStringProp(obj: Obj): Promise<Obj["stringProp"]> {
return Promise.resolve(obj.stringProp);
}
async function fIndexedTypeForExplicitPromiseOfStringProp(obj: Obj): Promise<Obj["stringProp"]> {
return Promise.resolve<Obj["stringProp"]>(obj.stringProp);
}
async function fIndexedTypeForAnyProp(obj: Obj): Promise<Obj["anyProp"]> {
return obj.anyProp;
}
async function fIndexedTypeForPromiseOfAnyProp(obj: Obj): Promise<Obj["anyProp"]> {
return Promise.resolve(obj.anyProp);
}
async function fIndexedTypeForExplicitPromiseOfAnyProp(obj: Obj): Promise<Obj["anyProp"]> {
return Promise.resolve<Obj["anyProp"]>(obj.anyProp);
}
async function fGenericIndexedTypeForStringProp<TObj extends Obj>(obj: TObj): Promise<TObj["stringProp"]> {
return obj.stringProp;
}
async function fGenericIndexedTypeForPromiseOfStringProp<TObj extends Obj>(obj: TObj): Promise<TObj["stringProp"]> {
return Promise.resolve(obj.stringProp);
}
async function fGenericIndexedTypeForExplicitPromiseOfStringProp<TObj extends Obj>(obj: TObj): Promise<TObj["stringProp"]> {
return Promise.resolve<TObj["stringProp"]>(obj.stringProp);
}
async function fGenericIndexedTypeForAnyProp<TObj extends Obj>(obj: TObj): Promise<TObj["anyProp"]> {
return obj.anyProp;
}
async function fGenericIndexedTypeForPromiseOfAnyProp<TObj extends Obj>(obj: TObj): Promise<TObj["anyProp"]> {
return Promise.resolve(obj.anyProp);
}
async function fGenericIndexedTypeForExplicitPromiseOfAnyProp<TObj extends Obj>(obj: TObj): Promise<TObj["anyProp"]> {
return Promise.resolve<TObj["anyProp"]>(obj.anyProp);
}
async function fGenericIndexedTypeForKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K]> {
return obj[key];
}
async function fGenericIndexedTypeForPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]> {
return Promise.resolve(obj[key]);
}
async function fGenericIndexedTypeForExplicitPromiseOfKProp<TObj extends Obj, K extends keyof TObj>(obj: TObj, key: K): Promise<TObj[K] extends PromiseLike<infer U> ? U : TObj[K]> {
return Promise.resolve<TObj[K]>(obj[key]);
}
//// [asyncFunctionReturnType.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
function fAsync() {
return __awaiter(this, void 0, void 0, function* () {
// Without explicit type annotation, this is just an array.
return [1, true];
});
}
function fAsyncExplicit() {
return __awaiter(this, void 0, void 0, function* () {
// This is contextually typed as a tuple.
return [1, true];
});
}
function fIndexedTypeForStringProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return obj.stringProp;
});
}
function fIndexedTypeForPromiseOfStringProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj.stringProp);
});
}
function fIndexedTypeForExplicitPromiseOfStringProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj.stringProp);
});
}
function fIndexedTypeForAnyProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return obj.anyProp;
});
}
function fIndexedTypeForPromiseOfAnyProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj.anyProp);
});
}
function fIndexedTypeForExplicitPromiseOfAnyProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj.anyProp);
});
}
function fGenericIndexedTypeForStringProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return obj.stringProp;
});
}
function fGenericIndexedTypeForPromiseOfStringProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj.stringProp);
});
}
function fGenericIndexedTypeForExplicitPromiseOfStringProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj.stringProp);
});
}
function fGenericIndexedTypeForAnyProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return obj.anyProp;
});
}
function fGenericIndexedTypeForPromiseOfAnyProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj.anyProp);
});
}
function fGenericIndexedTypeForExplicitPromiseOfAnyProp(obj) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj.anyProp);
});
}
function fGenericIndexedTypeForKProp(obj, key) {
return __awaiter(this, void 0, void 0, function* () {
return obj[key];
});
}
function fGenericIndexedTypeForPromiseOfKProp(obj, key) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj[key]);
});
}
function fGenericIndexedTypeForExplicitPromiseOfKProp(obj, key) {
return __awaiter(this, void 0, void 0, function* () {
return Promise.resolve(obj[key]);
});
}