Course contentsShow
← React
Lesson 2338 of 2,39651. TypeScript with React BasicsFree lesson

Typing Component Return Values

Understand JSX.Element, ReactElement, and ReactNode return types and when each applies.

Typing Component Return Values

What you'll learn: How to correctly type what your React components return in TypeScript, and which return type to use when.

The Three Return Types

When you write a TypeScript function component, you might wonder: what should I declare as its return type? TypeScript offers three main options, each with different meanings:

JSX.Element

The most restrictive type. It means your component must return a single JSX element—no null, undefined, or plain strings allowed.

function Greeting(): JSX.Element {
  return <h1>Hello</h1>; // ✓ Valid
  // return null; // ✗ Type error
}

ReactElement

Similar to JSX.Element but more technically precise. It's the underlying type that JSX expressions evaluate to. In practice, nearly identical to JSX.Element for component return values.

ReactNode

The most permissive type. Accepts anything React can render: JSX elements, strings, numbers, null, undefined, arrays, or fragments.

function FlexibleComponent(): ReactNode {
  return <div>Hello</div>; // ✓ Valid
  // return null; // ✓ Also valid
  // return "Plain text"; // ✓ Also valid
}

When to Use Each

JSX.Element: Use when your component always returns exactly one JSX element. Makes invalid returns a compile error.

ReactNode: Use when your component might return null (conditional rendering), strings, or fragments. This is the most common choice and matches React's actual behavior.

ReactElement: Rarely used explicitly—it's the technical type behind JSX, but JSX.Element or ReactNode are clearer choices.

Key Takeaway: Most components should return ReactNode for maximum flexibility with conditional rendering. Use JSX.Element only when you need to enforce that a component never returns null or primitives.