Add contextual type for generator return type (#39772)

* WIP

* Add contextual type for generator return type
This commit is contained in:
Andrew Branch
2020-07-27 15:14:47 -07:00
committed by GitHub
parent 5b2b70b2a5
commit 68ba670467
12 changed files with 284 additions and 12 deletions

View File

@@ -22977,18 +22977,25 @@ namespace ts {
function getContextualTypeForReturnExpression(node: Expression): Type | undefined {
const func = getContainingFunction(node);
if (func) {
const functionFlags = getFunctionFlags(func);
if (functionFlags & FunctionFlags.Generator) { // AsyncGenerator function or Generator function
return undefined;
}
const contextualReturnType = getContextualReturnType(func);
let contextualReturnType = getContextualReturnType(func);
if (contextualReturnType) {
if (functionFlags & FunctionFlags.Async) { // Async function
const contextualAwaitedType = mapType(contextualReturnType, getAwaitedTypeOfPromise);
const functionFlags = getFunctionFlags(func);
if (functionFlags & FunctionFlags.Generator) { // Generator or AsyncGenerator function
const use = functionFlags & FunctionFlags.Async ? IterationUse.AsyncGeneratorReturnType : IterationUse.GeneratorReturnType;
const iterationTypes = getIterationTypesOfIterable(contextualReturnType, use, /*errorNode*/ undefined);
if (!iterationTypes) {
return undefined;
}
contextualReturnType = iterationTypes.returnType;
// falls through to unwrap Promise for AsyncGenerators
}
if (functionFlags & FunctionFlags.Async) { // Async function or AsyncGenerator function
const contextualAwaitedType = mapType(contextualReturnType, getAwaitedType);
return contextualAwaitedType && getUnionType([contextualAwaitedType, createPromiseLikeType(contextualAwaitedType)]);
}
return contextualReturnType; // Regular function
return contextualReturnType; // Regular function or Generator function
}
}
return undefined;
@@ -33439,6 +33446,8 @@ namespace ts {
reportTypeNotIterableError(errorNode, type, !!(use & IterationUse.AllowsAsyncIterablesFlag));
errorNode = undefined;
}
setCachedIterationTypes(type, cacheKey, noIterationTypes);
return undefined;
}
else {
allIterationTypes = append(allIterationTypes, iterationTypes);