@@ -13198,16 +13198,11 @@ namespace ts {
13198
13198
// the type of the property with the numeric name N in T, if one exists. Otherwise, if T has a numeric index signature,
13199
13199
// it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated
13200
13200
// type of T.
13201
- function getContextualTypeForElementExpression(node: Expression): Type {
13202
- const arrayLiteral = <ArrayLiteralExpression>node.parent;
13203
- const type = getApparentTypeOfContextualType(arrayLiteral);
13204
- if (type) {
13205
- const index = indexOf(arrayLiteral.elements, node);
13206
- return getTypeOfPropertyOfContextualType(type, "" + index as __String)
13207
- || getIndexTypeOfContextualType(type, IndexKind.Number)
13208
- || getIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterables*/ false, /*checkAssignability*/ false);
13209
- }
13210
- return undefined;
13201
+ function getContextualTypeForElementExpression(arrayContextualType: Type | undefined, index: number): Type | undefined {
13202
+ return arrayContextualType && (
13203
+ getTypeOfPropertyOfContextualType(arrayContextualType, "" + index as __String)
13204
+ || getIndexTypeOfContextualType(arrayContextualType, IndexKind.Number)
13205
+ || getIteratedTypeOrElementType(arrayContextualType, /*errorNode*/ undefined, /*allowStringInput*/ false, /*allowAsyncIterables*/ false, /*checkAssignability*/ false));
13211
13206
}
13212
13207
13213
13208
// In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type.
@@ -13321,8 +13316,11 @@ namespace ts {
13321
13316
return getContextualTypeForObjectLiteralElement(<ObjectLiteralElementLike>parent);
13322
13317
case SyntaxKind.SpreadAssignment:
13323
13318
return getApparentTypeOfContextualType(parent.parent as ObjectLiteralExpression);
13324
- case SyntaxKind.ArrayLiteralExpression:
13325
- return getContextualTypeForElementExpression(node);
13319
+ case SyntaxKind.ArrayLiteralExpression: {
13320
+ const arrayLiteral = <ArrayLiteralExpression>parent;
13321
+ const type = getApparentTypeOfContextualType(arrayLiteral);
13322
+ return getContextualTypeForElementExpression(type, indexOfNode(arrayLiteral.elements, node));
13323
+ }
13326
13324
case SyntaxKind.ConditionalExpression:
13327
13325
return getContextualTypeForConditionalOperand(node);
13328
13326
case SyntaxKind.TemplateSpan:
@@ -13451,12 +13449,14 @@ namespace ts {
13451
13449
(node.kind === SyntaxKind.BinaryExpression && (<BinaryExpression>node).operatorToken.kind === SyntaxKind.EqualsToken);
13452
13450
}
13453
13451
13454
- function checkArrayLiteral(node: ArrayLiteralExpression, checkMode? : CheckMode): Type {
13452
+ function checkArrayLiteral(node: ArrayLiteralExpression, checkMode: CheckMode | undefined ): Type {
13455
13453
const elements = node.elements;
13456
13454
let hasSpreadElement = false;
13457
13455
const elementTypes: Type[] = [];
13458
13456
const inDestructuringPattern = isAssignmentTarget(node);
13459
- for (const e of elements) {
13457
+ const contextualType = getApparentTypeOfContextualType(node);
13458
+ for (let index = 0; index < elements.length; index++) {
13459
+ const e = elements[index];
13460
13460
if (inDestructuringPattern && e.kind === SyntaxKind.SpreadElement) {
13461
13461
// Given the following situation:
13462
13462
// var c: {};
@@ -13478,7 +13478,8 @@ namespace ts {
13478
13478
}
13479
13479
}
13480
13480
else {
13481
- const type = checkExpressionForMutableLocation(e, checkMode);
13481
+ const elementContextualType = getContextualTypeForElementExpression(contextualType, index);
13482
+ const type = checkExpressionForMutableLocation(e, checkMode, elementContextualType);
13482
13483
elementTypes.push(type);
13483
13484
}
13484
13485
hasSpreadElement = hasSpreadElement || e.kind === SyntaxKind.SpreadElement;
@@ -18023,9 +18024,13 @@ namespace ts {
18023
18024
return false;
18024
18025
}
18025
18026
18026
- function checkExpressionForMutableLocation(node: Expression, checkMode?: CheckMode): Type {
18027
+ function checkExpressionForMutableLocation(node: Expression, checkMode: CheckMode, contextualType?: Type): Type {
18028
+ if (arguments.length === 2) {
18029
+ contextualType = getContextualType(node);
18030
+ }
18027
18031
const type = checkExpression(node, checkMode);
18028
- return isTypeAssertion(node) || isLiteralContextualType(getContextualType(node)) ? type : getWidenedLiteralType(type);
18032
+ const shouldWiden = isTypeAssertion(node) || isLiteralContextualType(contextualType);
18033
+ return shouldWiden ? type : getWidenedLiteralType(type);
18029
18034
}
18030
18035
18031
18036
function checkPropertyAssignment(node: PropertyAssignment, checkMode?: CheckMode): Type {
0 commit comments