Skip to content

Commit d1a1b4d

Browse files
committedMay 26, 2021
Implements YieldType
1 parent 3e29397 commit d1a1b4d

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎src/compiler/checker.ts

+17
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ namespace ts {
883883
let globalBooleanType: ObjectType;
884884
let globalRegExpType: ObjectType;
885885
let globalThisType: GenericType;
886+
let globalYieldType: GenericType;
886887
let anyArrayType: Type;
887888
let autoArrayType: Type;
888889
let anyReadonlyArrayType: Type;
@@ -24931,6 +24932,16 @@ namespace ts {
2493124932
});
2493224933
}
2493324934

24935+
function getYieldTypeArgument(type: Type): Type | undefined {
24936+
return getObjectFlags(type) & ObjectFlags.Reference && (type as TypeReference).target === globalYieldType ? getTypeArguments(type as TypeReference)[0] : undefined;
24937+
}
24938+
24939+
function getYieldTypeFromContextualType(type: Type): Type | undefined {
24940+
return mapType(type, t => {
24941+
return t.flags & TypeFlags.Intersection ? forEach((t as IntersectionType).types, getYieldTypeArgument) : getYieldTypeArgument(t);
24942+
});
24943+
}
24944+
2493424945
function getContextualThisParameterType(func: SignatureDeclaration): Type | undefined {
2493524946
if (func.kind === SyntaxKind.ArrowFunction) {
2493624947
return undefined;
@@ -32326,6 +32337,11 @@ namespace ts {
3232632337
return getIterationTypeOfIterable(use, IterationTypeKind.Return, yieldExpressionType, node.expression)
3232732338
|| anyType;
3232832339
}
32340+
32341+
const suggestedReturnType = yieldedType && getYieldTypeFromContextualType(yieldedType);
32342+
if (suggestedReturnType) {
32343+
return suggestedReturnType;
32344+
}
3232932345
else if (returnType) {
3233032346
return getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Next, returnType, isAsync)
3233132347
|| anyType;
@@ -40618,6 +40634,7 @@ namespace ts {
4061840634
globalReadonlyArrayType = getGlobalTypeOrUndefined("ReadonlyArray" as __String, /*arity*/ 1) as GenericType || globalArrayType;
4061940635
anyReadonlyArrayType = globalReadonlyArrayType ? createTypeFromGenericGlobalType(globalReadonlyArrayType, [anyType]) : anyArrayType;
4062040636
globalThisType = getGlobalTypeOrUndefined("ThisType" as __String, /*arity*/ 1) as GenericType;
40637+
globalYieldType = getGlobalTypeOrUndefined("YieldType" as __String, /*arity*/ 1) as GenericType;
4062140638

4062240639
if (augmentations) {
4062340640
// merge _nonglobal_ module augmentations.

‎src/lib/es5.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,11 @@ type Uncapitalize<S extends string> = intrinsic;
15451545
*/
15461546
interface ThisType<T> { }
15471547

1548+
/**
1549+
* Marker for yield expressions return types
1550+
*/
1551+
interface YieldType<T> { }
1552+
15481553
/**
15491554
* Represents a raw buffer of binary data, which is used to store data for the
15501555
* different typed arrays. ArrayBuffers cannot be read from or written to directly,

0 commit comments

Comments
 (0)
Failed to load comments.