Fix contextual types for a single jsx child (#31040)

This commit is contained in:
Wesley Wigham
2019-04-23 13:51:39 -07:00
committed by GitHub
parent 54fa950757
commit b47194bfa1
7 changed files with 212 additions and 8 deletions

View File

@@ -11773,6 +11773,10 @@ namespace ts {
}
}
function getSemanticJsxChildren(children: NodeArray<JsxChild>) {
return filter(children, i => !isJsxText(i) || !i.containsOnlyTriviaWhiteSpaces);
}
function elaborateJsxComponents(node: JsxAttributes, source: Type, target: Type, relation: Map<RelationComparisonResult>) {
let result = elaborateElementwise(generateJsxAttributes(node), source, target, relation);
let invalidTextDiagnostic: DiagnosticMessage | undefined;
@@ -11782,7 +11786,7 @@ namespace ts {
const childrenPropName = childPropName === undefined ? "children" : unescapeLeadingUnderscores(childPropName);
const childrenNameType = getLiteralType(childrenPropName);
const childrenTargetType = getIndexedAccessType(target, childrenNameType);
const validChildren = filter(containingElement.children, i => !isJsxText(i) || !i.containsOnlyTriviaWhiteSpaces);
const validChildren = getSemanticJsxChildren(containingElement.children);
if (!length(validChildren)) {
return result;
}
@@ -18193,16 +18197,17 @@ namespace ts {
if (!(attributesType && !isTypeAny(attributesType) && jsxChildrenPropertyName && jsxChildrenPropertyName !== "")) {
return undefined;
}
const childIndex = node.children.indexOf(child);
const realChildren = getSemanticJsxChildren(node.children);
const childIndex = realChildren.indexOf(child);
const childFieldType = getTypeOfPropertyOfContextualType(attributesType, jsxChildrenPropertyName);
return childFieldType && mapType(childFieldType, t => {
return childFieldType && (realChildren.length === 1 ? childFieldType : mapType(childFieldType, t => {
if (isArrayLikeType(t)) {
return getIndexedAccessType(t, getLiteralType(childIndex));
}
else {
return t;
}
}, /*noReductions*/ true);
}, /*noReductions*/ true));
}
function getContextualTypeForJsxExpression(node: JsxExpression): Type | undefined {