@@ -3211,7 +3211,7 @@ namespace ts {
3211
3211
// This elementType will be used if the specific property corresponding to this index is not
3212
3212
// present (aka the tuple element property). This call also checks that the parentType is in
3213
3213
// fact an iterable or array (depending on target language).
3214
- const elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false);
3214
+ const elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false, /*allowAsyncIterable*/ false );
3215
3215
if (declaration.dotDotDotToken) {
3216
3216
// Rest element has an array type with the same element type as the parent type
3217
3217
type = createArrayType(elementType);
@@ -9512,12 +9512,12 @@ namespace ts {
9512
9512
9513
9513
function getTypeOfDestructuredArrayElement(type: Type, index: number) {
9514
9514
return isTupleLikeType(type) && getTypeOfPropertyOfType(type, "" + index) ||
9515
- checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) ||
9515
+ checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterable*/ false ) ||
9516
9516
unknownType;
9517
9517
}
9518
9518
9519
9519
function getTypeOfDestructuredSpreadExpression(type: Type) {
9520
- return createArrayType(checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || unknownType);
9520
+ return createArrayType(checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterable*/ false ) || unknownType);
9521
9521
}
9522
9522
9523
9523
function getAssignedTypeOfBinaryExpression(node: BinaryExpression): Type {
@@ -11410,7 +11410,7 @@ namespace ts {
11410
11410
const index = indexOf(arrayLiteral.elements, node);
11411
11411
return getTypeOfPropertyOfContextualType(type, "" + index)
11412
11412
|| getIndexTypeOfContextualType(type, IndexKind.Number)
11413
- || getIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*checkAssignability*/ false);
11413
+ || getIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterable*/ false, /* checkAssignability*/ false);
11414
11414
}
11415
11415
return undefined;
11416
11416
}
@@ -11628,7 +11628,7 @@ namespace ts {
11628
11628
}
11629
11629
11630
11630
const arrayOrIterableType = checkExpression(node.expression, contextualMapper);
11631
- return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false);
11631
+ return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false, /*allowAsyncIterable*/ false );
11632
11632
}
11633
11633
11634
11634
function hasDefaultValue(node: BindingElement | Expression): boolean {
@@ -11657,7 +11657,7 @@ namespace ts {
11657
11657
// if there is no index type / iterated type.
11658
11658
const restArrayType = checkExpression((<SpreadElement>e).expression, contextualMapper);
11659
11659
const restElementType = getIndexTypeOfType(restArrayType, IndexKind.Number) ||
11660
- getIteratedTypeOrElementType(restArrayType, /*errorNode*/ undefined, /*allowStringInput*/ false, /*checkAssignability*/ false);
11660
+ getIteratedTypeOrElementType(restArrayType, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterable*/ false, /* checkAssignability*/ false);
11661
11661
if (restElementType) {
11662
11662
elementTypes.push(restElementType);
11663
11663
}
@@ -14359,9 +14359,7 @@ namespace ts {
14359
14359
let type = checkExpressionCached(expr, contextualMapper);
14360
14360
if (yieldExpression.asteriskToken) {
14361
14361
// A yield* expression effectively yields everything that its operand yields
14362
- type = functionFlags & FunctionFlags.Async
14363
- ? checkIteratedTypeOfAsyncIterable(type, yieldExpression.expression) // AsyncGenerator function
14364
- : checkIteratedTypeOrElementType(type, yieldExpression.expression, /*allowStringInput*/ false); // Generator function
14362
+ type = checkIteratedTypeOrElementType(type, yieldExpression.expression, /*allowStringInput*/ false, (functionFlags & FunctionFlags.Async) !== 0);
14365
14363
}
14366
14364
if (!contains(aggregatedTypes, type)) {
14367
14365
aggregatedTypes.push(type);
@@ -14914,7 +14912,7 @@ namespace ts {
14914
14912
// This elementType will be used if the specific property corresponding to this index is not
14915
14913
// present (aka the tuple element property). This call also checks that the parentType is in
14916
14914
// fact an iterable or array (depending on target language).
14917
- const elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false) || unknownType;
14915
+ const elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false, /*allowAsyncIterable*/ false ) || unknownType;
14918
14916
const elements = node.elements;
14919
14917
for (let i = 0; i < elements.length; i++) {
14920
14918
checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, elementType, contextualMapper);
@@ -15339,9 +15337,7 @@ namespace ts {
15339
15337
let expressionElementType: Type;
15340
15338
const nodeIsYieldStar = !!node.asteriskToken;
15341
15339
if (nodeIsYieldStar) {
15342
- expressionElementType = functionFlags & FunctionFlags.Async
15343
- ? checkIteratedTypeOfAsyncIterable(expressionType, node.expression) // AsyncGenerator function
15344
- : checkIteratedTypeOrElementType(expressionType, node.expression, /*allowStringInput*/ false); // Generator function
15340
+ expressionElementType = checkIteratedTypeOrElementType(expressionType, node.expression, /*allowStringInput*/ false, (functionFlags & FunctionFlags.Async) !== 0);
15345
15341
}
15346
15342
15347
15343
// There is no point in doing an assignability check if the function
@@ -17936,41 +17932,32 @@ namespace ts {
17936
17932
17937
17933
function checkRightHandSideOfForOf(rhsExpression: Expression, awaitModifier: AwaitKeywordToken | undefined): Type {
17938
17934
const expressionType = checkNonNullExpression(rhsExpression);
17939
- return awaitModifier
17940
- ? checkIteratedTypeOfAsyncIterable(expressionType, rhsExpression)
17941
- : checkIteratedTypeOrElementType(expressionType, rhsExpression, /*allowStringInput*/ true);
17935
+ return checkIteratedTypeOrElementType(expressionType, rhsExpression, /*allowStringInput*/ true, awaitModifier !== undefined);
17942
17936
}
17943
17937
17944
- function checkIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean): Type {
17938
+ function checkIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean, allowAsyncIterable: boolean ): Type {
17945
17939
if (isTypeAny(inputType)) {
17946
17940
return inputType;
17947
17941
}
17948
17942
17949
- return getIteratedTypeOrElementType(inputType, errorNode, allowStringInput, /*checkAssignability*/ true) || anyType;
17950
- }
17951
-
17952
- function checkIteratedTypeOfAsyncIterable(inputType: Type, errorNode: Node): Type {
17953
- if (isTypeAny(inputType)) {
17954
- return inputType;
17955
- }
17956
-
17957
- return getIteratedTypeOfIterable(inputType, errorNode, /*isAsyncIterable*/ true, /*allowNonAsyncIterables*/ true, /*checkAssignability*/ true) || anyType;
17943
+ return getIteratedTypeOrElementType(inputType, errorNode, allowStringInput, allowAsyncIterable, /*checkAssignability*/ true) || anyType;
17958
17944
}
17959
17945
17960
17946
/**
17961
17947
* When consuming an iterable type in a for..of, spread, or iterator destructuring assignment
17962
17948
* we want to get the iterated type of an iterable for ES2015 or later, or the iterated type
17963
17949
* of a iterable (if defined globally) or element type of an array like for ES2015 or earlier.
17964
17950
*/
17965
- function getIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean, checkAssignability: boolean): Type {
17951
+ function getIteratedTypeOrElementType(inputType: Type, errorNode: Node, allowStringInput: boolean, allowAsyncIterable: boolean, checkAssignability: boolean): Type {
17966
17952
const uplevelIteration = languageVersion >= ScriptTarget.ES2015;
17967
17953
const downlevelIteration = !uplevelIteration && compilerOptions.downlevelIteration;
17968
17954
17969
17955
// Get the iterated type of an `Iterable<T>` or `IterableIterator<T>` only in ES2015
17970
- // or higher, or when downlevelIteration is supplied.
17971
- if (uplevelIteration || downlevelIteration) {
17956
+ // or higher, when inside of an async generator or for-await-if, or when
17957
+ // downlevelIteration is requested.
17958
+ if (uplevelIteration || downlevelIteration || allowAsyncIterable) {
17972
17959
// We only report errors for an invalid iterable type in ES2015 or higher.
17973
- const iteratedType = getIteratedTypeOfIterable(inputType, uplevelIteration ? errorNode : undefined, /*isAsyncIterable*/ false, /*allowNonAsyncIterables*/ false , checkAssignability);
17960
+ const iteratedType = getIteratedTypeOfIterable(inputType, uplevelIteration ? errorNode : undefined, allowAsyncIterable, allowAsyncIterable , checkAssignability);
17974
17961
if (iteratedType || uplevelIteration) {
17975
17962
return iteratedType;
17976
17963
}
@@ -20458,7 +20445,7 @@ namespace ts {
20458
20445
Debug.assert(expr.parent.kind === SyntaxKind.ArrayLiteralExpression);
20459
20446
// [{ property1: p1, property2 }] = elems;
20460
20447
const typeOfArrayLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(<Expression>expr.parent);
20461
- const elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false) || unknownType;
20448
+ const elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false, /*allowAsyncIterable*/ false ) || unknownType;
20462
20449
return checkArrayLiteralDestructuringElementAssignment(<ArrayLiteralExpression>expr.parent, typeOfArrayLiteral,
20463
20450
indexOf((<ArrayLiteralExpression>expr.parent).elements, expr), elementType || unknownType);
20464
20451
}
0 commit comments