Skip to content

Commit 95bfd7c

Browse files
committed
Assignability checking for yield and yield* expressions
1 parent 623507c commit 95bfd7c

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7905,6 +7905,26 @@ module ts {
79057905
grammarErrorOnFirstToken(node, Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_declaration);
79067906
}
79077907

7908+
if (node.expression) {
7909+
let func = getContainingFunction(node);
7910+
// If this is correct code, the func should always have a star. After all,
7911+
// we are in a yield context.
7912+
// Also, there is no point in doing an assignability check if the function
7913+
// has no explicit return type, because the return type is directly computed
7914+
// from the yield expressions.
7915+
if (func.asteriskToken && func.type) {
7916+
let signatureElementType = getElementTypeFromIterableIterator(getTypeFromTypeNode(func.type), /*errorNode*/ undefined) || unknownType;
7917+
let expressionType = checkExpressionCached(node.expression, /*contextualMapper*/ undefined);
7918+
if (node.asteriskToken) {
7919+
let expressionElementType = checkIteratedType(expressionType, node.expression);
7920+
checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, /*headMessage*/ undefined);
7921+
}
7922+
else {
7923+
checkTypeAssignableTo(expressionType, signatureElementType, node.expression, /*headMessage*/ undefined);
7924+
}
7925+
}
7926+
}
7927+
79087928
// Both yield and yield* expressions are any
79097929
return anyType;
79107930
}
@@ -9480,7 +9500,7 @@ module ts {
94809500
}
94819501

94829502
if (languageVersion >= ScriptTarget.ES6) {
9483-
return checkIteratedType(inputType, errorNode) || anyType;
9503+
return checkIteratedType(inputType, errorNode);
94849504
}
94859505

94869506
if (allowStringInput) {
@@ -9509,7 +9529,7 @@ module ts {
95099529
checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode);
95109530
}
95119531

9512-
return elementType;
9532+
return elementType || anyType;
95139533
}
95149534

95159535
function getElementTypeFromIterable(iterable: Type, errorNode: Node): Type {

0 commit comments

Comments
 (0)