Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yield Overrides #47548

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,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 @@ -25875,6 +25876,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 @@ -33603,6 +33614,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 @@ -42399,6 +42415,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
1 change: 1 addition & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,7 @@ namespace FourSlashInterface {
typeEntry("Capitalize"),
typeEntry("Uncapitalize"),
interfaceEntry("ThisType"),
interfaceEntry("YieldType"),
varEntry("ArrayBuffer"),
interfaceEntry("ArrayBufferTypes"),
typeEntry("ArrayBufferLike"),
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 @@ -1605,6 +1605,11 @@ type Uncapitalize<S extends string> = intrinsic;
*/
interface ThisType<T> { }

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

Choose a reason for hiding this comment

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

Maybe use type YieldType<T> = intrinsic;? (see #46836 for ThisType above)


/**
* 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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function* g007() { // Generator<never, void, unknown>
>g007 : () => Generator<never, void, unknown>

yield never;
>yield never : any
>yield never : never
>never : never
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function* g007() { // Generator<never, void, unknown>
>g007 : () => Generator<never, void, unknown>

yield never;
>yield never : any
>yield never : never
>never : never
}

Expand Down