8000 GitHub · Where software is built
[go: up one dir, main page]

Skip to content
Parse error with ternary in JSX with mixed variables and JSX #24121
Closed
@Guria

Description

@Guria

TypeScript Version: 2.9.0-dev.20180515

Search Terms: ternary, ternary tsx

Code

Here is working code

import * as React from "react";
const emptyMessage = <div>empty</div>;
const length = 0;
const totalItemsCount = 10;
const getPlural = (...args: any[]) => "";
const a = (
  <div>
    <div>
      {length ? emptyMessage : (
        <span>
          Всего {totalItemsCount}{" "}
          {getPlural(totalItemsCount, ["услуга", "услуги", "услуг"])} в этом
          разделе
        </span>
      )}
    </div>
  </div>
);

after applying prettier on this code we have:

import * as React from "react";
const emptyMessage = <div>empty</div>;
const length = 0;
const totalItemsCount = 10;
const getPlural = (...args: any[]) => "";
const a = (
  <div>
    <div>
      {length ? (
        emptyMessage
      ) : (
        <span>
          Всего {totalItemsCount}{" "}
          {getPlural(totalItemsCount, ["услуга", "услуги", "услуг"])} в этом
          разделе
        </span>
      )}
    </div>
  </div>
);

As we see prettier added a parenthesis around emptyMessage which shouldn't affect parsing since this is a valid syntax. But typescript reports error:

[ts] A function whose declared type is neither 'void' nor 'any' must return a value.

The interesting part here, that if we simplify else part it will start work again:

import * as React from "react";
const emptyMessage = <div>empty</div>;
const length = 0;
const totalItemsCount = 10;
const getPlural = (...args: any[]) => "";
const a = (
  <div>
    <div>
      {length ? (
        emptyMessage
      ) : (
        <span>
          {getPlural(totalItemsCount, ["услуга", "услуги", "услуг"])} в этом
          разделе
        </span>
      )}
    </div>
  </div>
);

It appears enough to remove first line Всего {totalItemsCount}{" "} inside span where we have string literal, and 2 expressions.
I tried to change this first line in different combinations (just literal, just expressions, etc) but only complete removal of this exact line helps. o_O

Expected behavior: Compile in any case above

Actual behavior: Some cases led to parsing error

Playground Link: https://codesandbox.io/s/3z83kpxp1 (hit Prettify icon in top right corner under profile menu)

Related Issues: #16241

I am starting issue here and not in prettier repo, since I am sure that all snippets above are valid syntax,

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0