Fixed an issue with generic naked T not being allowed as async generator's return (#49023)

This commit is contained in:
Mateusz Burzyński 2022-07-15 00:51:03 +02:00 committed by GitHub
parent a21024dbe7
commit cd3bd5522b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 3 deletions

View File

@ -39273,9 +39273,14 @@ namespace ts {
function unwrapReturnType(returnType: Type, functionFlags: FunctionFlags) {
const isGenerator = !!(functionFlags & FunctionFlags.Generator);
const isAsync = !!(functionFlags & FunctionFlags.Async);
return isGenerator ? getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, returnType, isAsync) || errorType :
isAsync ? getAwaitedTypeNoAlias(returnType) || errorType :
returnType;
if (isGenerator) {
const returnIterationType = getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Return, returnType, isAsync);
if (!returnIterationType) {
return errorType;
}
return isAsync ? getAwaitedTypeNoAlias(unwrapAwaitedType(returnIterationType)) : returnIterationType;
}
return isAsync ? getAwaitedTypeNoAlias(returnType) || errorType : returnType;
}
function isUnwrappedReturnTypeVoidOrAny(func: SignatureDeclaration, returnType: Type): boolean {

View File

@ -0,0 +1,17 @@
=== tests/cases/conformance/generators/asyncGeneratorGenericNonWrappedReturn.ts ===
// #48966
export async function* test<T>(a: T): AsyncGenerator<T, T, T> {
>test : Symbol(test, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 0, 0))
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
>a : Symbol(a, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 31))
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
>AsyncGenerator : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --))
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
>T : Symbol(T, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 28))
return a // `T` should be allowed here even though the generator's `returnType` is `Awaited<T>`
>a : Symbol(a, Decl(asyncGeneratorGenericNonWrappedReturn.ts, 2, 31))
}

View File

@ -0,0 +1,11 @@
=== tests/cases/conformance/generators/asyncGeneratorGenericNonWrappedReturn.ts ===
// #48966
export async function* test<T>(a: T): AsyncGenerator<T, T, T> {
>test : <T>(a: T) => AsyncGenerator<T, T, T>
>a : T
return a // `T` should be allowed here even though the generator's `returnType` is `Awaited<T>`
>a : T
}

View File

@ -0,0 +1,9 @@
// @target: esnext
// @strict: true
// @noEmit: true
// #48966
export async function* test<T>(a: T): AsyncGenerator<T, T, T> {
return a // `T` should be allowed here even though the generator's `returnType` is `Awaited<T>`
}