diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7bbab7005fd..18ad0c0437c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13318,8 +13318,13 @@ namespace ts { for (const child of (parent as JsxElement).children) { // In React, JSX text that contains only whitespaces will be ignored so we don't want to type-check that // because then type of children property will have constituent of string type. - if (child.kind !== SyntaxKind.JsxTextAllWhiteSpaces) { - childrenTypes.push(child.kind === SyntaxKind.JsxText ? stringType : checkExpression(child as Expression, checkMode)); + if (child.kind === SyntaxKind.JsxText) { + if (!child.containsOnlyWhiteSpaces) { + childrenTypes.push(stringType); + } + } + else { + childrenTypes.push(checkExpression(child, checkMode)); } } childrenPropSymbol.type = getUnionType(childrenTypes, /*subtypeReduction*/ false); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 770601bd56c..cc18fb3260c 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3825,7 +3825,8 @@ namespace ts { } function parseJsxText(): JsxText { - const node = createNode(currentToken, scanner.getStartPos()); + const node = createNode(SyntaxKind.JsxText, scanner.getStartPos()); + node.containsOnlyWhiteSpaces = currentToken === SyntaxKind.JsxTextAllWhiteSpaces; currentToken = scanner.scanJsxToken(); return finishNode(node); } diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 59453f4fa4b..2d7ca500893 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -1729,7 +1729,6 @@ namespace ts { // firstNonWhitespace = 0 to indicate that we want leading whitspace, while (pos < end) { - pos++; char = text.charCodeAt(pos); if (char === CharacterCodes.openBrace) { break; @@ -1754,6 +1753,7 @@ namespace ts { else if (!isWhiteSpaceSingleLine(char)) { firstNonWhitespace = pos; } + pos++; } return firstNonWhitespace === -1 ? SyntaxKind.JsxTextAllWhiteSpaces : SyntaxKind.JsxText; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 0e0deeedfaa..d52f277c9ef 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1572,7 +1572,8 @@ } export interface JsxText extends Node { - kind: SyntaxKind.JsxText; + kind: SyntaxKind.JsxText, + containsOnlyWhiteSpaces: boolean, parent?: JsxElement; }