Skip to content

Commit f55167e

Browse files
author
Kanchalai Tanglertsampan
committed
Collect type from return statment in generator function
1 parent 359823b commit f55167e

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14963,10 +14963,12 @@ namespace ts {
1496314963
}
1496414964
}
1496514965
else {
14966-
let types: Type[];
14966+
let yieldTypes: Type[];
14967+
let returnTypes: Type[];
1496714968
if (functionFlags & FunctionFlags.Generator) { // Generator or AsyncGenerator function
14968-
types = checkAndAggregateYieldOperandTypes(func, contextualMapper);
14969-
if (types.length === 0) {
14969+
yieldTypes = checkAndAggregateYieldOperandTypes(func, contextualMapper);
14970+
returnTypes = checkAndAggregateReturnExpressionTypes(func, contextualMapper);
14971+
if (yieldTypes.length === 0 && (!returnTypes || returnTypes.length === 0)) {
1497014972
const iterableIteratorAny = functionFlags & FunctionFlags.Async
1497114973
? createAsyncIterableIteratorType(anyType) // AsyncGenerator function
1497214974
: createIterableIteratorType(anyType); // Generator function
@@ -14978,22 +14980,22 @@ namespace ts {
1497814980
}
1497914981
}
1498014982
else {
14981-
types = checkAndAggregateReturnExpressionTypes(func, contextualMapper);
14982-
if (!types) {
14983+
returnTypes = checkAndAggregateReturnExpressionTypes(func, contextualMapper);
14984+
if (!returnTypes) {
1498314985
// For an async function, the return type will not be never, but rather a Promise for never.
1498414986
return functionFlags & FunctionFlags.Async
1498514987
? createPromiseReturnType(func, neverType) // Async function
1498614988
: neverType; // Normal function
1498714989
}
14988-
if (types.length === 0) {
14990+
if (returnTypes.length === 0) {
1498914991
// For an async function, the return type will not be void, but rather a Promise for void.
1499014992
return functionFlags & FunctionFlags.Async
1499114993
? createPromiseReturnType(func, voidType) // Async function
1499214994
: voidType; // Normal function
1499314995
}
1499414996
}
1499514997
// Return a union of the return expression types.
14996-
type = getUnionType(types, /*subtypeReduction*/ true);
14998+
type = getUnionType(yieldTypes ? yieldTypes.concat(returnTypes) : returnTypes, /*subtypeReduction*/ true);
1499714999

1499815000
if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
1499915001
type = functionFlags & FunctionFlags.Async

0 commit comments

Comments
 (0)