mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-20 05:17:43 -05:00
Add contextual type for generator return type (#39772)
* WIP * Add contextual type for generator return type
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user