@@ -12635,6 +12635,35 @@ namespace ts {
12635
12635
return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined;
12636
12636
}
12637
12637
12638
+ function getContextualTypeForJsxExpression(node: JsxExpression): Type {
12639
+ // JSX expression can appear in two position : JSX Element's children or JSX attribute
12640
+ const jsxAttributes = isJsxAttributeLike(node.parent) ?
12641
+ node.parent.parent :
12642
+ node.parent.openingElement.attributes; // node.parent is JsxElement
12643
+
12644
+ // When we trying to resolve JsxOpeningLikeElement as a stateless function element, we will already give its attributes a contextual type
12645
+ // which is a type of the parameter of the signature we are trying out.
12646
+ // If there is no contextual type (e.g. we are trying to resolve stateful component), get attributes type from resolving element's tagName
12647
+ const attributesType = getContextualType(jsxAttributes);
12648
+
12649
+ if (!attributesType || isTypeAny(attributesType)) {
12650
+ return undefined;
12651
+ }
12652
+
12653
+ if (isJsxAttribute(node.parent)) {
12654
+ // JSX expression is in JSX attribute
12655
+ return getTypeOfPropertyOfType(attributesType, (node.parent as JsxAttribute).name.text);
12656
+ }
12657
+ else if (node.parent.kind === SyntaxKind.JsxElement) {
12658
+ // JSX expression is in children of JSX Element, we will look for an atttribute named "chidlren"
12659
+ return getTypeOfPropertyOfType(attributesType, jsxChildrenPropertyName);
12660
+ }
12661
+ else {
12662
+ // JSX expression is in JSX spread attribute
12663
+ return attributesType;
12664
+ }
12665
+ }
12666
+
12638
12667
function getContextualTypeForJsxAttribute(attribute: JsxAttribute | JsxSpreadAttribute) {
12639
12668
// When we trying to resolve JsxOpeningLikeElement as a stateless function element, we will already give its attributes a contextual type
12640
12669
// which is a type of the parameter of the signature we are trying out.
@@ -12718,7 +12747,7 @@ namespace ts {
12718
12747
case SyntaxKind.ParenthesizedExpression:
12719
12748
return getContextualType(<ParenthesizedExpression>parent);
12720
12749
case SyntaxKind.JsxExpression:
12721
- return getContextualType (<JsxExpression>parent);
12750
+ return getContextualTypeForJsxExpression (<JsxExpression>parent);
12722
12751
case SyntaxKind.JsxAttribute:
12723
12752
case SyntaxKind.JsxSpreadAttribute:
12724
12753
return getContextualTypeForJsxAttribute(<JsxAttribute | JsxSpreadAttribute>parent);
0 commit comments