Skip to content

Commit

Permalink
Implements YieldType
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis committed May 26, 2021
1 parent 3e29397 commit d1a1b4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ namespace ts {
let globalBooleanType: ObjectType;
let globalRegExpType: ObjectType;
let globalThisType: GenericType;
let globalYieldType: GenericType;
let anyArrayType: Type;
let autoArrayType: Type;
let anyReadonlyArrayType: Type;
Expand Down Expand Up @@ -24931,6 +24932,16 @@ namespace ts {
});
}

function getYieldTypeArgument(type: Type): Type | undefined {
return getObjectFlags(type) & ObjectFlags.Reference && (type as TypeReference).target === globalYieldType ? getTypeArguments(type as TypeReference)[0] : undefined;
}

function getYieldTypeFromContextualType(type: Type): Type | undefined {
return mapType(type, t => {
return t.flags & TypeFlags.Intersection ? forEach((t as IntersectionType).types, getYieldTypeArgument) : getYieldTypeArgument(t);
});
}

function getContextualThisParameterType(func: SignatureDeclaration): Type | undefined {
if (func.kind === SyntaxKind.ArrowFunction) {
return undefined;
Expand Down Expand Up @@ -32326,6 +32337,11 @@ namespace ts {
return getIterationTypeOfIterable(use, IterationTypeKind.Return, yieldExpressionType, node.expression)
|| anyType;
}

const suggestedReturnType = yieldedType && getYieldTypeFromContextualType(yieldedType);
if (suggestedReturnType) {
return suggestedReturnType;
}
else if (returnType) {
return getIterationTypeOfGeneratorFunctionReturnType(IterationTypeKind.Next, returnType, isAsync)
|| anyType;
Expand Down Expand Up @@ -40618,6 +40634,7 @@ namespace ts {
globalReadonlyArrayType = getGlobalTypeOrUndefined("ReadonlyArray" as __String, /*arity*/ 1) as GenericType || globalArrayType;
anyReadonlyArrayType = globalReadonlyArrayType ? createTypeFromGenericGlobalType(globalReadonlyArrayType, [anyType]) : anyArrayType;
globalThisType = getGlobalTypeOrUndefined("ThisType" as __String, /*arity*/ 1) as GenericType;
globalYieldType = getGlobalTypeOrUndefined("YieldType" as __String, /*arity*/ 1) as GenericType;

if (augmentations) {
// merge _nonglobal_ module augmentations.
Expand Down
5 changes: 5 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,11 @@ type Uncapitalize<S extends string> = intrinsic;
*/
interface ThisType<T> { }

/**
* Marker for yield expressions return types
*/
interface YieldType<T> { }

/**
* Represents a raw buffer of binary data, which is used to store data for the
* different typed arrays. ArrayBuffers cannot be read from or written to directly,
Expand Down

1 comment on commit d1a1b4d

@Andarist
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I would never have expected that so few lines would be required to add support for this 😅

Please sign in to comment.