Skip to content

Commit 968d062

Browse files
Port missing nil check in getContextualTypeForReturnExpression (#3755)
1 parent ea005f3 commit 968d062

6 files changed

Lines changed: 99 additions & 0 deletions

File tree

internal/checker/checker.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28938,6 +28938,9 @@ func (c *Checker) getContextualTypeForReturnExpression(node *ast.Node, contextFl
2893828938
if functionFlags&ast.FunctionFlagsAsync != 0 {
2893928939
// Get the awaited type without the `Awaited<T>` alias
2894028940
contextualAwaitedType := c.mapType(contextualReturnType, c.getAwaitedTypeNoAlias)
28941+
if contextualAwaitedType == nil {
28942+
return nil
28943+
}
2894128944
return c.getUnionType([]*Type{contextualAwaitedType, c.createPromiseLikeType(contextualAwaitedType)})
2894228945
}
2894328946
// Regular function or Generator function
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
asyncFunctionReturnNonPromiseThenable.ts(5,30): 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>'?
2+
asyncFunctionReturnNonPromiseThenable.ts(6,5): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member.
3+
4+
5+
==== asyncFunctionReturnNonPromiseThenable.ts (2 errors) ====
6+
export interface MyThenable {
7+
then(): void;
8+
}
9+
10+
export async function foo(): MyThenable {
11+
~~~~~~~~~~
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>'?
13+
return {
14+
~~~~~~
15+
!!! error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member.
16+
then() {
17+
}
18+
};
19+
}
20+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/asyncFunctionReturnNonPromiseThenable.ts] ////
2+
3+
//// [asyncFunctionReturnNonPromiseThenable.ts]
4+
export interface MyThenable {
5+
then(): void;
6+
}
7+
8+
export async function foo(): MyThenable {
9+
return {
10+
then() {
11+
}
12+
};
13+
}
14+
15+
16+
//// [asyncFunctionReturnNonPromiseThenable.js]
17+
export async function foo() {
18+
return {
19+
then() {
20+
}
21+
};
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/asyncFunctionReturnNonPromiseThenable.ts] ////
2+
3+
=== asyncFunctionReturnNonPromiseThenable.ts ===
4+
export interface MyThenable {
5+
>MyThenable : Symbol(MyThenable, Decl(asyncFunctionReturnNonPromiseThenable.ts, 0, 0))
6+
7+
then(): void;
8+
>then : Symbol(MyThenable.then, Decl(asyncFunctionReturnNonPromiseThenable.ts, 0, 29))
9+
}
10+
11+
export async function foo(): MyThenable {
12+
>foo : Symbol(foo, Decl(asyncFunctionReturnNonPromiseThenable.ts, 2, 1))
13+
>MyThenable : Symbol(MyThenable, Decl(asyncFunctionReturnNonPromiseThenable.ts, 0, 0))
14+
15+
return {
16+
then() {
17+
>then : Symbol(then, Decl(asyncFunctionReturnNonPromiseThenable.ts, 5, 12))
18+
}
19+
};
20+
}
21+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/compiler/asyncFunctionReturnNonPromiseThenable.ts] ////
2+
3+
=== asyncFunctionReturnNonPromiseThenable.ts ===
4+
export interface MyThenable {
5+
then(): void;
6+
>then : () => void
7+
}
8+
9+
export async function foo(): MyThenable {
10+
>foo : () => MyThenable
11+
12+
return {
13+
>{ then() { } } : { then(): void; }
14+
15+
then() {
16+
>then : () => void
17+
}
18+
};
19+
}
20+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @strict: true
2+
// @target: esnext
3+
4+
export interface MyThenable {
5+
then(): void;
6+
}
7+
8+
export async function foo(): MyThenable {
9+
return {
10+
then() {
11+
}
12+
};
13+
}

0 commit comments

Comments
 (0)