Skip to content

Commit a0ef319

Browse files
Simplified JSX contextual typing code and added support for contextually typing string literal attribute initializers.
1 parent 4dc85dc commit a0ef319

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

src/compiler/checker.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7464,24 +7464,22 @@ namespace ts {
74647464
return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined;
74657465
}
74667466

7467-
function getContextualTypeForJsxExpression(expr: JsxExpression | JsxSpreadAttribute): Type {
7468-
// Contextual type only applies to JSX expressions that are in attribute assignments (not in 'Children' positions)
7469-
if (expr.parent.kind === SyntaxKind.JsxAttribute) {
7470-
const attrib = <JsxAttribute>expr.parent;
7471-
const attrsType = getJsxElementAttributesType(<JsxOpeningLikeElement>attrib.parent);
7467+
function getContextualTypeForJsxAttribute(attribute: JsxAttribute | JsxSpreadAttribute) {
7468+
const kind = attribute.kind;
7469+
const jsxElement = attribute.parent as JsxOpeningLikeElement;
7470+
const attrsType = getJsxElementAttributesType(jsxElement);
7471+
7472+
if (attribute.kind === SyntaxKind.JsxAttribute) {
74727473
if (!attrsType || isTypeAny(attrsType)) {
74737474
return undefined;
74747475
}
7475-
else {
7476-
return getTypeOfPropertyOfType(attrsType, attrib.name.text);
7477-
}
7476+
return getTypeOfPropertyOfType(attrsType, (attribute as JsxAttribute).name.text);
74787477
}
7479-
7480-
if (expr.kind === SyntaxKind.JsxSpreadAttribute) {
7481-
return getJsxElementAttributesType(<JsxOpeningLikeElement>expr.parent);
7478+
else if (attribute.kind === SyntaxKind.JsxSpreadAttribute) {
7479+
return attrsType;
74827480
}
74837481

7484-
return undefined;
7482+
Debug.fail(`Expected JsxAttribute or JsxSpreadAttribute, got ts.SyntaxKind[${kind}]`);
74857483
}
74867484

74877485
// Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily
@@ -7549,8 +7547,10 @@ namespace ts {
75497547
case SyntaxKind.ParenthesizedExpression:
75507548
return getContextualType(<ParenthesizedExpression>parent);
75517549
case SyntaxKind.JsxExpression:
7550+
return getContextualType(<JsxExpression>parent);
7551+
case SyntaxKind.JsxAttribute:
75527552
case SyntaxKind.JsxSpreadAttribute:
7553-
return getContextualTypeForJsxExpression(<JsxExpression>parent);
7553+
return getContextualTypeForJsxAttribute(<JsxAttribute | JsxSpreadAttribute>parent);
75547554
}
75557555
return undefined;
75567556
}

0 commit comments

Comments
 (0)