Skip to content

Commit a4cddd4

Browse files
authored
Merge pull request #32239 from andrewbranch/enhancement/missing-await-errors
Improve error messages for potentially missing 'await'
2 parents 949956b + bdd8a3e commit a4cddd4

9 files changed

+607
-44
lines changed

src/compiler/checker.ts

Lines changed: 125 additions & 41 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,10 @@
26932693
"category": "Error",
26942694
"code": 2772
26952695
},
2696+
"Did you forget to use 'await'?": {
2697+
"category": "Error",
2698+
"code": 2773
2699+
},
26962700

26972701
"Import declaration '{0}' is using private name '{1}'.": {
26982702
"category": "Error",
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(11,9): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
2+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(12,5): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
3+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(13,5): error TS2365: Operator '+' cannot be applied to types 'number' and 'Promise<number>'.
4+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(14,5): error TS2365: Operator '>' cannot be applied to types 'number' and 'Promise<number>'.
5+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(15,5): error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
6+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(16,7): error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
7+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(17,5): error TS2367: This condition will always return 'false' since the types 'number' and 'Promise<number>' have no overlap.
8+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(18,9): error TS2461: Type 'Promise<string[]>' is not an array type.
9+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(19,21): error TS2495: Type 'Promise<string[]>' is not an array type or a string type.
10+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(20,12): error TS2345: Argument of type 'Promise<number>' is not assignable to parameter of type 'number'.
11+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(21,11): error TS2570: Property 'prop' does not exist on type 'Promise<{ prop: string; }>'. Did you forget to use 'await'?
12+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(23,27): error TS2495: Type 'Promise<string[]>' is not an array type or a string type.
13+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(24,5): error TS2349: This expression is not callable.
14+
Type 'Promise<() => void>' has no call signatures.
15+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(25,5): error TS2349: This expression is not callable.
16+
Not all constituents of type 'Promise<() => void> | (() => void)' are callable.
17+
Type 'Promise<() => void>' has no call signatures.
18+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(26,9): error TS2351: This expression is not constructable.
19+
Type 'Promise<new () => any>' has no construct signatures.
20+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(27,5): error TS2349: This expression is not callable.
21+
Type 'Promise<number>' has no call signatures.
22+
23+
24+
==== tests/cases/compiler/operationsAvailableOnPromisedType.ts (16 errors) ====
25+
async function fn(
26+
a: number,
27+
b: Promise<number>,
28+
c: Promise<string[]>,
29+
d: Promise<{ prop: string }>,
30+
e: Promise<() => void>,
31+
f: Promise<() => void> | (() => void),
32+
g: Promise<{ new(): any }>
33+
) {
34+
// All errors
35+
a | b;
36+
~
37+
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
38+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:11:9: Did you forget to use 'await'?
39+
b | a;
40+
~
41+
!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
42+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:12:5: Did you forget to use 'await'?
43+
a + b;
44+
~~~~~
45+
!!! error TS2365: Operator '+' cannot be applied to types 'number' and 'Promise<number>'.
46+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:13:5: Did you forget to use 'await'?
47+
a > b;
48+
~~~~~
49+
!!! error TS2365: Operator '>' cannot be applied to types 'number' and 'Promise<number>'.
50+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:14:5: Did you forget to use 'await'?
51+
b++;
52+
~
53+
!!! error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
54+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:15:5: Did you forget to use 'await'?
55+
--b;
56+
~
57+
!!! error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
58+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:16:7: Did you forget to use 'await'?
59+
a === b;
60+
~~~~~~~
61+
!!! error TS2367: This condition will always return 'false' since the types 'number' and 'Promise<number>' have no overlap.
62+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:17:5: Did you forget to use 'await'?
63+
[...c];
64+
~
65+
!!! error TS2461: Type 'Promise<string[]>' is not an array type.
66+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:18:9: Did you forget to use 'await'?
67+
for (const s of c) {
68+
~
69+
!!! error TS2495: Type 'Promise<string[]>' is not an array type or a string type.
70+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:19:21: Did you forget to use 'await'?
71+
fn(b, b, c, d, e, f, g);
72+
~
73+
!!! error TS2345: Argument of type 'Promise<number>' is not assignable to parameter of type 'number'.
74+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:20:12: Did you forget to use 'await'?
75+
d.prop;
76+
~~~~
77+
!!! error TS2570: Property 'prop' does not exist on type 'Promise<{ prop: string; }>'. Did you forget to use 'await'?
78+
}
79+
for await (const s of c) {}
80+
~
81+
!!! error TS2495: Type 'Promise<string[]>' is not an array type or a string type.
82+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:23:27: Did you forget to use 'await'?
83+
e();
84+
~
85+
!!! error TS2349: This expression is not callable.
86+
!!! error TS2349: Type 'Promise<() => void>' has no call signatures.
87+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:24:5: Did you forget to use 'await'?
88+
f();
89+
~
90+
!!! error TS2349: This expression is not callable.
91+
!!! error TS2349: Not all constituents of type 'Promise<() => void> | (() => void)' are callable.
92+
!!! error TS2349: Type 'Promise<() => void>' has no call signatures.
93+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:25:5: Did you forget to use 'await'?
94+
new g();
95+
~
96+
!!! error TS2351: This expression is not constructable.
97+
!!! error TS2351: Type 'Promise<new () => any>' has no construct signatures.
98+
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:26:9: Did you forget to use 'await'?
99+
b();
100+
~
101+
!!! error TS2349: This expression is not callable.
102+
!!! error TS2349: Type 'Promise<number>' has no call signatures.
103+
}
104+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//// [operationsAvailableOnPromisedType.ts]
2+
async function fn(
3+
a: number,
4+
b: Promise<number>,
5+
c: Promise<string[]>,
6+
d: Promise<{ prop: string }>,
7+
e: Promise<() => void>,
8+
f: Promise<() => void> | (() => void),
9+
g: Promise<{ new(): any }>
10+
) {
11+
// All errors
12+
a | b;
13+
b | a;
14+
a + b;
15+
a > b;
16+
b++;
17+
--b;
18+
a === b;
19+
[...c];
20+
for (const s of c) {
21+
fn(b, b, c, d, e, f, g);
22+
d.prop;
23+
}
24+
for await (const s of c) {}
25+
e();
26+
f();
27+
new g();
28+
b();
29+
}
30+
31+
32+
//// [operationsAvailableOnPromisedType.js]
33+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
34+
return new (P || (P = Promise))(function (resolve, reject) {
35+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
36+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
37+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
38+
step((generator = generator.apply(thisArg, _arguments || [])).next());
39+
});
40+
};
41+
var __generator = (this && this.__generator) || function (thisArg, body) {
42+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
43+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
44+
function verb(n) { return function (v) { return step([n, v]); }; }
45+
function step(op) {
46+
if (f) throw new TypeError("Generator is already executing.");
47+
while (_) try {
48+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
49+
if (y = 0, t) op = [op[0] & 2, t.value];
50+
switch (op[0]) {
51+
case 0: case 1: t = op; break;
52+
case 4: _.label++; return { value: op[1], done: false };
53+
case 5: _.label++; y = op[1]; op = [0]; continue;
54+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
55+
default:
56+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
57+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
58+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
59+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
60+
if (t[2]) _.ops.pop();
61+
_.trys.pop(); continue;
62+
}
63+
op = body.call(thisArg, _);
64+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
65+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
66+
}
67+
};
68+
var __asyncValues = (this && this.__asyncValues) || function (o) {
69+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
70+
var m = o[Symbol.asyncIterator], i;
71+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
72+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
73+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
74+
};
75+
var __spreadArrays = (this && this.__spreadArrays) || function () {
76+
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
77+
for (var r = Array(s), k = 0, i = 0; i < il; i++)
78+
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
79+
r[k] = a[j];
80+
return r;
81+
};
82+
function fn(a, b, c, d, e, f, g) {
83+
var c_1, c_1_1;
84+
var e_1, _a;
85+
return __awaiter(this, void 0, void 0, function () {
86+
var _i, c_2, s, s, e_1_1;
87+
return __generator(this, function (_b) {
88+
switch (_b.label) {
89+
case 0:
90+
// All errors
91+
a | b;
92+
b | a;
93+
a + b;
94+
a > b;
95+
b++;
96+
--b;
97+
a === b;
98+
__spreadArrays(c);
99+
for (_i = 0, c_2 = c; _i < c_2.length; _i++) {
100+
s = c_2[_i];
101+
fn(b, b, c, d, e, f, g);
102+
d.prop;
103+
}
104+
_b.label = 1;
105+
case 1:
106+
_b.trys.push([1, 6, 7, 12]);
107+
c_1 = __asyncValues(c);
108+
_b.label = 2;
109+
case 2: return [4 /*yield*/, c_1.next()];
110+
case 3:
111+
if (!(c_1_1 = _b.sent(), !c_1_1.done)) return [3 /*break*/, 5];
112+
s = c_1_1.value;
113+
_b.label = 4;
114+
case 4: return [3 /*break*/, 2];
115+
case 5: return [3 /*break*/, 12];
116+
case 6:
117+
e_1_1 = _b.sent();
118+
e_1 = { error: e_1_1 };
119+
return [3 /*break*/, 12];
120+
case 7:
121+
_b.trys.push([7, , 10, 11]);
122+
if (!(c_1_1 && !c_1_1.done && (_a = c_1["return"]))) return [3 /*break*/, 9];
123+
return [4 /*yield*/, _a.call(c_1)];
124+
case 8:
125+
_b.sent();
126+
_b.label = 9;
127+
case 9: return [3 /*break*/, 11];
128+
case 10:
129+
if (e_1) throw e_1.error;
130+
return [7 /*endfinally*/];
131+
case 11: return [7 /*endfinally*/];
132+
case 12:
133+
e();
134+
f();
135+
new g();
136+
b();
137+
return [2 /*return*/];
138+
}
139+
});
140+
});
141+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
=== tests/cases/compiler/operationsAvailableOnPromisedType.ts ===
2+
async function fn(
3+
>fn : Symbol(fn, Decl(operationsAvailableOnPromisedType.ts, 0, 0))
4+
5+
a: number,
6+
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
7+
8+
b: Promise<number>,
9+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
10+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))
11+
12+
c: Promise<string[]>,
13+
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
14+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))
15+
16+
d: Promise<{ prop: string }>,
17+
>d : Symbol(d, Decl(operationsAvailableOnPromisedType.ts, 3, 25))
18+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))
19+
>prop : Symbol(prop, Decl(operationsAvailableOnPromisedType.ts, 4, 16))
20+
21+
e: Promise<() => void>,
22+
>e : Symbol(e, Decl(operationsAvailableOnPromisedType.ts, 4, 33))
23+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))
24+
25+
f: Promise<() => void> | (() => void),
26+
>f : Symbol(f, Decl(operationsAvailableOnPromisedType.ts, 5, 27))
27+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))
28+
29+
g: Promise<{ new(): any }>
30+
>g : Symbol(g, Decl(operationsAvailableOnPromisedType.ts, 6, 42))
31+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))
32+
33+
) {
34+
// All errors
35+
a | b;
36+
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
37+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
38+
39+
b | a;
40+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
41+
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
42+
43+
a + b;
44+
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
45+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
46+
47+
a > b;
48+
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
49+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
50+
51+
b++;
52+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
53+
54+
--b;
55+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
56+
57+
a === b;
58+
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
59+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
60+
61+
[...c];
62+
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
63+
64+
for (const s of c) {
65+
>s : Symbol(s, Decl(operationsAvailableOnPromisedType.ts, 18, 14))
66+
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
67+
68+
fn(b, b, c, d, e, f, g);
69+
>fn : Symbol(fn, Decl(operationsAvailableOnPromisedType.ts, 0, 0))
70+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
71+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
72+
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
73+
>d : Symbol(d, Decl(operationsAvailableOnPromisedType.ts, 3, 25))
74+
>e : Symbol(e, Decl(operationsAvailableOnPromisedType.ts, 4, 33))
75+
>f : Symbol(f, Decl(operationsAvailableOnPromisedType.ts, 5, 27))
76+
>g : Symbol(g, Decl(operationsAvailableOnPromisedType.ts, 6, 42))
77+
78+
d.prop;
79+
>d : Symbol(d, Decl(operationsAvailableOnPromisedType.ts, 3, 25))
80+
}
81+
for await (const s of c) {}
82+
>s : Symbol(s, Decl(operationsAvailableOnPromisedType.ts, 22, 20))
83+
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
84+
85+
e();
86+
>e : Symbol(e, Decl(operationsAvailableOnPromisedType.ts, 4, 33))
87+
88+
f();
89+
>f : Symbol(f, Decl(operationsAvailableOnPromisedType.ts, 5, 27))
90+
91+
new g();
92+
>g : Symbol(g, Decl(operationsAvailableOnPromisedType.ts, 6, 42))
93+
94+
b();
95+
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
96+
}
97+

0 commit comments

Comments
 (0)