Skip to content

Commit cd3bd55

Browse files
authored
Fixed an issue with generic naked T not being allowed as async generator's return (#49023)
1 parent a21024d commit cd3bd55

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39273,9 +39273,14 @@ namespace ts {
3927339273
function unwrapReturnType(returnType: Type, functionFlags: FunctionFlags) {
3927439274
const isGenerator = !!(functionFlags & FunctionFlags.Generator);
3927539275
const isAsync = !!(functionFlags & FunctionFlags.Async);
39276-
return isGenerator ? getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, returnType, isAsync) || errorType :
39277-
isAsync ? getAwaitedTypeNoAlias(returnType) || errorType :
39278-
returnType;
39276+
if (isGenerator) {
39277+
const returnIterationType = getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, returnType, isAsync);
39278+
if (!returnIterationType) {
39279+
return errorType;
39280+
}
39281+
return isAsync ? getAwaitedTypeNoAlias(unwrapAwaitedType(returnIterationType)) : returnIterationType;
39282+
}
39283+
return isAsync ? getAwaitedTypeNoAlias(returnType) || errorType : returnType;
3927939284
}
3928039285

3928139286
function isUnwrappedReturnTypeVoidOrAny(func: SignatureDeclaration, returnType: Type): boolean {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/generators/asyncGeneratorGenericNonWrappedReturn.ts ===
2+
// #48966
3+
4+
export async function* test<T>(a: T): AsyncGenerator<T, T, T> {
5+
>test : Symbol(test, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 0, 0))
6+
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
7+
>a : Symbol(a, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 31))
8+
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
9+
>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --))
10+
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
11+
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
12+
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
13+
14+
return a // `T` should be allowed here even though the generator's `returnType` is `Awaited<T>`
15+
>a : Symbol(a, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 31))
16+
}
17+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/conformance/generators/asyncGeneratorGenericNonWrappedReturn.ts ===
2+
// #48966
3+
4+
export async function* test<T>(a: T): AsyncGenerator<T, T, T> {
5+
>test : <T>(a: T) => AsyncGenerator<T, T, T>
6+
>a : T
7+
8+
return a // `T` should be allowed here even though the generator's `returnType` is `Awaited<T>`
9+
>a : T
10+
}
11+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @target: esnext
2+
// @strict: true
3+
// @noEmit: true
4+
5+
// #48966
6+
7+
export async function* test<T>(a: T): AsyncGenerator<T, T, T> {
8+
return a // `T` should be allowed here even though the generator's `returnType` is `Awaited<T>`
9+
}

0 commit comments

Comments
 (0)