Skip to content

Commit a813d63

Browse files
committed
feat(36266): add a quick fix for incorrect return types in async functions
1 parent 6d7539a commit a813d63

19 files changed

+194
-16
lines changed

src/compiler/checker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ namespace ts {
403403
},
404404
getParameterType: getTypeAtPosition,
405405
getPromisedTypeOfPromise,
406+
getAwaitedType: type => getAwaitedType(type),
406407
getReturnTypeOfSignature,
407408
isNullableType,
408409
getNullableType,
@@ -30354,7 +30355,7 @@ namespace ts {
3035430355
if (globalPromiseType !== emptyGenericType && !isReferenceToType(returnType, globalPromiseType)) {
3035530356
// The promise type was not a valid type reference to the global promise type, so we
3035630357
// report an error and return the unknown type.
30357-
error(returnTypeNode, Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type);
30358+
error(returnTypeNode, Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type_Did_you_mean_to_write_Promise_0, typeToString(getAwaitedType(returnType) || voidType));
3035830359
return;
3035930360
}
3036030361
}

src/compiler/diagnosticMessages.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
"category": "Error",
212212
"code": 1063
213213
},
214-
"The return type of an async function or method must be the global Promise<T> type.": {
214+
"The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{0}>'?": {
215215
"category": "Error",
216216
"code": 1064
217217
},
@@ -5141,6 +5141,14 @@
51415141
"category": "Message",
51425142
"code": 90035
51435143
},
5144+
"Replace '{0}' with 'Promise<{1}>'": {
5145+
"category": "Message",
5146+
"code": 90036
5147+
},
5148+
"Fix all incorrect return type of an async functions": {
5149+
"category": "Message",
5150+
"code": 90037
5151+
},
51445152
"Declare a private field named '{0}'.": {
51455153
"category": "Message",
51465154
"code": 90053

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3421,6 +3421,7 @@ namespace ts {
34213421
getWidenedType(type: Type): Type;
34223422
/* @internal */
34233423
getPromisedTypeOfPromise(promise: Type, errorNode?: Node): Type | undefined;
3424+
getAwaitedType(type: Type): Type | undefined;
34243425
getReturnTypeOfSignature(signature: Signature): Type;
34253426
/**
34263427
* Gets the type of a parameter at a given position in a signature.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
const fixId = "fixReturnTypeInAsyncFunction";
4+
const errorCodes = [
5+
Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type_Did_you_mean_to_write_Promise_0.code,
6+
];
7+
8+
registerCodeFix({
9+
errorCodes,
10+
fixIds: [fixId],
11+
getCodeActions: context => {
12+
const { sourceFile, program, span } = context;
13+
const checker = program.getTypeChecker();
14+
const info = getInfo(sourceFile, program.getTypeChecker(), span.start);
15+
if (!info) {
16+
return undefined;
17+
}
18+
const { returnTypeNode, returnType, promisedTypeNode, promisedType } = info;
19+
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, returnTypeNode, promisedTypeNode));
20+
return [createCodeFixAction(fixId, changes, [Diagnostics.Replace_0_with_Promise_1, checker.typeToString(returnType), checker.typeToString(promisedType)], fixId, Diagnostics.Fix_all_incorrect_return_type_of_an_async_functions)];
21+
},
22+
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
23+
const info = getInfo(diag.file, context.program.getTypeChecker(), diag.start);
24+
if (info) {
25+
doChange(changes, diag.file, info.returnTypeNode, info.promisedTypeNode);
26+
}
27+
})
28+
});
29+
30+
function getInfo(sourceFile: SourceFile, checker: TypeChecker, pos: number) {
31+
const returnTypeNode = getReturnTypeNode(sourceFile, pos);
32+
if (!returnTypeNode) {
33+
return undefined;
34+
}
35+
36+
const returnType = checker.getTypeFromTypeNode(returnTypeNode);
37+
const promisedType = checker.getAwaitedType(returnType) || checker.getVoidType();
38+
const promisedTypeNode = checker.typeToTypeNode(promisedType);
39+
if (promisedTypeNode) {
40+
return { returnTypeNode, returnType, promisedTypeNode, promisedType };
41+
}
42+
}
43+
44+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, returnTypeNode: TypeNode, promisedTypeNode: TypeNode): void {
45+
changes.replaceNode(sourceFile, returnTypeNode, createTypeReferenceNode("Promise", [promisedTypeNode]));
46+
}
47+
48+
function getReturnTypeNode(sourceFile: SourceFile, pos: number): TypeNode | undefined {
49+
if (isInJSFile(sourceFile)) {
50+
return undefined;
51+
}
52+
53+
const token = getTokenAtPosition(sourceFile, pos);
54+
const parent = findAncestor(token, isFunctionLikeDeclaration);
55+
if (parent?.type) {
56+
return parent.type;
57+
}
58+
}
59+
}

src/services/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"codefixes/fixMissingCallParentheses.ts",
8585
"codefixes/fixAwaitInSyncFunction.ts",
8686
"codefixes/inferFromUsage.ts",
87+
"codefixes/fixReturnTypeInAsyncFunction.ts",
8788
"codefixes/disableJsDiagnostics.ts",
8889
"codefixes/helpers.ts",
8990
"codefixes/fixInvalidImportSyntax.ts",

tests/baselines/reference/api/tsserverlibrary.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,7 @@ declare namespace ts {
20352035
getBaseTypes(type: InterfaceType): BaseType[];
20362036
getBaseTypeOfLiteralType(type: Type): Type;
20372037
getWidenedType(type: Type): Type;
2038+
getAwaitedType(type: Type): Type | undefined;
20382039
getReturnTypeOfSignature(signature: Signature): Type;
20392040
getNullableType(type: Type, flags: TypeFlags): Type;
20402041
getNonNullableType(type: Type): Type;

tests/baselines/reference/api/typescript.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,7 @@ declare namespace ts {
20352035
getBaseTypes(type: InterfaceType): BaseType[];
20362036
getBaseTypeOfLiteralType(type: Type): Type;
20372037
getWidenedType(type: Type): Type;
2038+
getAwaitedType(type: Type): Type | undefined;
20382039
getReturnTypeOfSignature(signature: Signature): Type;
20392040
getNullableType(type: Type, flags: TypeFlags): Type;
20402041
getNonNullableType(type: Type): Type;

tests/baselines/reference/asyncFunctionDeclaration15_es6.errors.txt

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
1+
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{}>'?
22
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
3-
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(7,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
4-
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
3+
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(7,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<any>'?
4+
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<number>'?
55
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
6-
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(9,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
7-
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(10,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
6+
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(9,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
7+
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(10,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
88
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(17,16): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member.
99
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(23,25): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.
1010

@@ -17,23 +17,23 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration1
1717
async function fn1() { } // valid: Promise<void>
1818
async function fn2(): { } { } // error
1919
~~~
20-
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
20+
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{}>'?
2121
~~~
2222
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
2323
async function fn3(): any { } // error
2424
~~~
25-
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
25+
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<any>'?
2626
async function fn4(): number { } // error
2727
~~~~~~
28-
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
28+
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<number>'?
2929
~~~~~~
3030
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
3131
async function fn5(): PromiseLike<void> { } // error
3232
~~~~~~~~~~~~~~~~~
33-
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
33+
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
3434
async function fn6(): Thenable { } // error
3535
~~~~~~~~
36-
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
36+
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
3737
async function fn7() { return; } // valid: Promise<void>
3838
async function fn8() { return 1; } // valid: Promise<number>
3939
async function fn9() { return null; } // valid: Promise<any>

tests/baselines/reference/asyncImportedPromise_es6.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/async/es6/test.ts(3,25): error TS1064: The return type of an async function or method must be the global Promise<T> type.
1+
tests/cases/conformance/async/es6/test.ts(3,25): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<T>'?
22

33

44
==== tests/cases/conformance/async/es6/task.ts (0 errors) ====
@@ -9,5 +9,5 @@ tests/cases/conformance/async/es6/test.ts(3,25): error TS1064: The return type o
99
class Test {
1010
async example<T>(): Task<T> { return; }
1111
~~~~~~~
12-
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
12+
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<T>'?
1313
}

tests/baselines/reference/asyncQualifiedReturnType_es6.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/async/es6/asyncQualifiedReturnType_es6.ts(6,21): error TS1064: The return type of an async function or method must be the global Promise<T> type.
1+
tests/cases/conformance/async/es6/asyncQualifiedReturnType_es6.ts(6,21): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
22

33

44
==== tests/cases/conformance/async/es6/asyncQualifiedReturnType_es6.ts (1 errors) ====
@@ -9,5 +9,5 @@ tests/cases/conformance/async/es6/asyncQualifiedReturnType_es6.ts(6,21): error T
99

1010
async function f(): X.MyPromise<void> {
1111
~~~~~~~~~~~~~~~~~
12-
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
12+
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
1313
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////async function foo(): number {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
9+
newFileContent: `async function foo(): Promise<number> {}`
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////async function a(): number {}
5+
////async function b(): string {}
6+
////async function c(): string {}
7+
8+
verify.codeFixAll({
9+
fixId: "fixReturnTypeInAsyncFunction",
10+
fixAllDescription: ts.Diagnostics.Fix_all_incorrect_return_type_of_an_async_functions.message,
11+
newFileContent:
12+
`async function a(): Promise<number> {}
13+
async function b(): Promise<string> {}
14+
async function c(): Promise<string> {}`
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////const foo = async (): number => {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
9+
newFileContent: `const foo = async (): Promise<number> => {}`
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////const foo = async function (): number {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
9+
newFileContent: `const foo = async function (): Promise<number> {}`
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////class A {
5+
//// async foo(): number {}
6+
////}
7+
8+
verify.codeFix({
9+
index: 0,
10+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
11+
newFileContent:
12+
`class A {
13+
async foo(): Promise<number> {}
14+
}`
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////interface Foo<T> {}
5+
////async function foo(): Foo<number> {}
6+
7+
verify.codeFix({
8+
index: 0,
9+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "Foo<number>", "Foo<number>"],
10+
newFileContent:
11+
`interface Foo<T> {}
12+
async function foo(): Promise<Foo<number>> {}`
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////async function foo(): PromiseLike<void> {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "PromiseLike<void>", "void"],
9+
newFileContent: `async function foo(): Promise<void> {}`
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////async function foo(): {} {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "{}", "{}"],
9+
newFileContent: `async function foo(): Promise<{}> {}`
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////declare class Thenable { then(): void; }
5+
////async function foo(): Thenable {}
6+
7+
verify.codeFix({
8+
index: 0,
9+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "Thenable", "void"],
10+
newFileContent:
11+
`declare class Thenable { then(): void; }
12+
async function foo(): Promise<void> {}`
13+
});

0 commit comments

Comments
 (0)