Skip to content

Check return type inside generators #23571

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 7 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23219,14 +23219,15 @@ namespace ts {
const returnType = getReturnTypeOfSignature(signature);
const functionFlags = getFunctionFlags(func);
const isGenerator = functionFlags & FunctionFlags.Generator;
const isAsync = (functionFlags & FunctionFlags.Async) !== 0;
if (strictNullChecks || node.expression || returnType.flags & TypeFlags.Never) {
const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType;
if (isGenerator) { // AsyncGenerator function or Generator function
// A generator does not need its return expressions checked against its return type.
// Instead, the yield expressions are checked against the element type.
// TODO: Check return types of generators when return type tracking is added
// for generators.
return;
if (isGenerator) {
const returnType = getEffectiveReturnTypeNode(func);
if (returnType) {
const signatureElementType = getIteratedTypeOfGenerator(getTypeFromTypeNode(returnType), isAsync) || anyType;
checkTypeAssignableTo(exprType, signatureElementType, node);
}
}
else if (func.kind === SyntaxKind.SetAccessor) {
if (node.expression) {
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/generatorTypeCheck12.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck12.ts(2,5): error TS2322: Type '""' is not assignable to type 'number'.


==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck12.ts (1 errors) ====
function* g(): IterableIterator<number> {
return "";
~~~~~~~~~~
!!! error TS2322: Type '""' is not assignable to type 'number'.
}
10 changes: 10 additions & 0 deletions tests/baselines/reference/generatorTypeCheck13.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck13.ts(3,5): error TS2322: Type '""' is not assignable to type 'number'.


==== tests/cases/conformance/es6/yieldExpressions/generatorTypeCheck13.ts (1 errors) ====
function* g(): IterableIterator<number> {
yield 0;
return "";
~~~~~~~~~~
!!! error TS2322: Type '""' is not assignable to type 'number'.
}