Skip to content

Commit 93aef86

Browse files
committed
feat(36266): add a quick fix for incorrect return types in async functions
1 parent 52dc9f2 commit 93aef86

28 files changed

+329
-16
lines changed

Diff for: src/compiler/checker.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ namespace ts {
404404
},
405405
getParameterType: getTypeAtPosition,
406406
getPromisedTypeOfPromise,
407+
getAwaitedType: type => getAwaitedType(type),
407408
getReturnTypeOfSignature,
408409
isNullableType,
409410
getNullableType,
@@ -30558,7 +30559,7 @@ namespace ts {
3055830559
if (globalPromiseType !== emptyGenericType && !isReferenceToType(returnType, globalPromiseType)) {
3055930560
// The promise type was not a valid type reference to the global promise type, so we
3056030561
// report an error and return the unknown type.
30561-
error(returnTypeNode, Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type);
30562+
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));
3056230563
return;
3056330564
}
3056430565
}

Diff for: src/compiler/diagnosticMessages.json

+9-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@
215215
"category": "Error",
216216
"code": 1063
217217
},
218-
"The return type of an async function or method must be the global Promise<T> type.": {
218+
"The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{0}>'?": {
219219
"category": "Error",
220220
"code": 1064
221221
},
@@ -5181,6 +5181,14 @@
51815181
"category": "Message",
51825182
"code": 90035
51835183
},
5184+
"Replace '{0}' with 'Promise<{1}>'": {
5185+
"category": "Message",
5186+
"code": 90036
5187+
},
5188+
"Fix all incorrect return type of an async functions": {
5189+
"category": "Message",
5190+
"code": 90037
5191+
},
51845192
"Declare a private field named '{0}'.": {
51855193
"category": "Message",
51865194
"code": 90053

Diff for: 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.
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
interface Info {
9+
readonly returnTypeNode: TypeNode;
10+
readonly returnType: Type;
11+
readonly promisedTypeNode: TypeNode;
12+
readonly promisedType: Type;
13+
}
14+
15+
registerCodeFix({
16+
errorCodes,
17+
fixIds: [fixId],
18+
getCodeActions: context => {
19+
const { sourceFile, program, span } = context;
20+
const checker = program.getTypeChecker();
21+
const info = getInfo(sourceFile, program.getTypeChecker(), span.start);
22+
if (!info) {
23+
return undefined;
24+
}
25+
const { returnTypeNode, returnType, promisedTypeNode, promisedType } = info;
26+
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, returnTypeNode, promisedTypeNode));
27+
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)];
28+
},
29+
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
30+
const info = getInfo(diag.file, context.program.getTypeChecker(), diag.start);
31+
if (info) {
32+
doChange(changes, diag.file, info.returnTypeNode, info.promisedTypeNode);
33+
}
34+
})
35+
});
36+
37+
function getInfo(sourceFile: SourceFile, checker: TypeChecker, pos: number): Info | undefined {
38+
const returnTypeNode = getReturnTypeNode(sourceFile, pos);
39+
if (!returnTypeNode) {
40+
return undefined;
41+
}
42+
43+
const returnType = checker.getTypeFromTypeNode(returnTypeNode);
44+
const promisedType = checker.getAwaitedType(returnType) || checker.getVoidType();
45+
const promisedTypeNode = checker.typeToTypeNode(promisedType);
46+
if (promisedTypeNode) {
47+
return { returnTypeNode, returnType, promisedTypeNode, promisedType };
48+
}
49+
}
50+
51+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, returnTypeNode: TypeNode, promisedTypeNode: TypeNode): void {
52+
changes.replaceNode(sourceFile, returnTypeNode, createTypeReferenceNode("Promise", [promisedTypeNode]));
53+
}
54+
55+
function getReturnTypeNode(sourceFile: SourceFile, pos: number): TypeNode | undefined {
56+
if (isInJSFile(sourceFile)) {
57+
return undefined;
58+
}
59+
60+
const token = getTokenAtPosition(sourceFile, pos);
61+
const parent = findAncestor(token, isFunctionLikeDeclaration);
62+
if (parent?.type) {
63+
return parent.type;
64+
}
65+
}
66+
}

Diff for: src/services/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"codefixes/fixMissingCallParentheses.ts",
8686
"codefixes/fixAwaitInSyncFunction.ts",
8787
"codefixes/inferFromUsage.ts",
88+
"codefixes/fixReturnTypeInAsyncFunction.ts",
8889
"codefixes/disableJsDiagnostics.ts",
8990
"codefixes/helpers.ts",
9091
"codefixes/fixInvalidImportSyntax.ts",

Diff for: 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;

Diff for: 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;

Diff for: 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>

Diff for: 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
}

Diff for: 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 fn(): number {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
9+
newFileContent: `async function fn(): Promise<number> {}`
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @target: es2015
4+
////type Foo = "a" | "b";
5+
////async function fn(): Foo {}
6+
7+
verify.codeFix({
8+
index: 0,
9+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "Foo", "Foo"],
10+
newFileContent:
11+
`type Foo = "a" | "b";
12+
async function fn(): Promise<Foo> {}`
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 fn(): PromiseLike<string> {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "PromiseLike<string>", "string"],
9+
newFileContent: `async function fn(): Promise<string> {}`
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 fn(): 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 fn(): Promise<void> {}`
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 fn(): 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 fn(): Promise<void> {}`
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 fn(): string | symbol {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "string | symbol", "string | symbol"],
9+
newFileContent: `async function fn(): Promise<string | symbol> {}`
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,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+
////async function fn(): boolean {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "boolean", "boolean"],
9+
newFileContent: `async function fn(): Promise<boolean> {}`
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 fn(): string {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "string", "string"],
9+
newFileContent: `async function fn(): Promise<string> {}`
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 fn(): void {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "void", "void"],
9+
newFileContent: `async function fn(): 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 fn(): null {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "null", "null"],
9+
newFileContent: `async function fn(): Promise<null> {}`
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 fn(): undefined {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "undefined", "undefined"],
9+
newFileContent: `async function fn(): Promise<undefined> {}`
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 fn(): any {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "any", "any"],
9+
newFileContent: `async function fn(): Promise<any> {}`
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 fn(): symbol {}
5+
6+
verify.codeFix({
7+
index: 0,
8+
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "symbol", "symbol"],
9+
newFileContent: `async function fn(): Promise<symbol> {}`
10+
});

0 commit comments

Comments
 (0)