Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript 2695: left side of comma operator is unused in React code

ErrorLeft side of comma operator is unused and has no side effects.ts(2695)

Short answer

In JavaScript a comma between expressions evaluates all of them and keeps only the last, so the left ones are thrown away. TypeScript flags that. In React it nearly always means two JSX elements or two values were joined with a comma where a wrapper or separate expressions were meant.

  • Applies to TypeScript 5.9, React 19, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the line the editor underlines and look at the comma. Ask whether the thing on its left is something you expected to see in the result.

    npx tsc --noEmit
  2. 2Match what you see, then jump to its fix

    1. 1return (<Header />, <Main />) or two elements in one pair of parentheses separated by a commaTwo JSX elements are returned with a comma between themFix
    2. 2<p>{firstName, lastName}</p> or a prop like value={a, b}Two values are written inside one pair of curly bracesFix
    3. 3const x = (a, b); or an arrow function body like () => (setOpen(true), count) that was meant to hold two valuesA declaration is wrapped in parentheses, so the comma becomes an operatorFix

What does the error look like?

  • Left side of comma operator is unused and has no side effects.ts(2695)

Cause 1 of 3

Two JSX elements are returned with a comma between them

1Check

Only the last operand of a comma expression is returned, so Header is evaluated and dropped. A component also may not return two sibling JSX tags without a wrapper.

grep -rn "return (<" src | grep "/>,"

2Fix

Delete the comma and wrap the siblings in a Fragment, or in a div if you need an element.

return (
  <>
    <Header />
    <Main />
  </>
);

3Verify

Run the type check; the error is gone and both components render.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

Two values are written inside one pair of curly braces

1Check

Curly braces in JSX hold a single JavaScript expression. With a comma inside them, only the value after the last comma is used and the first is evaluated and discarded.

grep -rnE "\{[a-zA-Z_.]+, [a-zA-Z_.]+\}" src --include=*.tsx

2Fix

Make it one expression, for example a template string, or use two separate pairs of braces.

<p>{`${firstName} ${lastName}`}</p>
<p>{firstName} {lastName}</p>

3Verify

Both names show on the page and tsc is clean.

npx tsc --noEmit

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

A declaration is wrapped in parentheses, so the comma becomes an operator

1Check

A comma that separates declarations in const a = 1, b = 2 is not the comma operator. Put the pair in parentheses and it becomes one, and the first value is discarded.

grep -rnE "= \([a-zA-Z_]+, " src --include=*.ts --include=*.tsx

2Fix

If you want two values, use an array or object. If you want two separate variables, drop the parentheses.

const pair = [a, b];
const first = a, second = b;

3Verify

The variables hold what you expect and the error disappears.

npx tsc --noEmit

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Wrap sibling JSX in a Fragment as soon as you add a second root element.
  • Run tsc --noEmit or the editor's diagnostics before committing.
  • Use an array or an object when a function must return two values.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript says a JavaScript module implicitly has an any type

Could not find a declaration file for module 'react-materialize'. 'path/to/module-name.js' implicitly has an 'any' type.

TypeScript found the JavaScript file but no types for it, and noImplicitAny turns that into an error.

2 causes, each with a fix

Web apps

TypeScript says a function lacks an ending return statement

A function with a declared return type, such as Promise<string>, shows the red line under the type

TypeScript found a way for your function to reach its closing brace without returning a value, while the declared return type does not allow undefined.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.