Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
Dev How To
Logo
Dev How To

Table of Contents

  • Command Line
    • Man, Info and Help
    • Shell Built-In Commands
    • Shell Argument Syntax
    • End Of Options ‘–’
    • Echo and Printf
    • Redirections
    • Bash Brace Expansion
    • Bash Parameter Expansion
    • Bash Aliases
    • Bash Arrays
    • du Command
    • Intro
    • grep - Global Regular Expression and Print
    • tar | Command Line
    • Find files and store in array
    • File Conversion
    • HackerRank Shell Challenges and Solutions
    • The End
  • Git
    • Git Commit Messages
    • More Git Commit Message Examples
    • Git Commit Messages
    • Bad Git Commit Messages
    • Git Branches: push, pull, merge and rebase
    • Git Branching
    • Git Merge
    • Git Reset
    • Git Revisions
    • Inspecting a Git Repository
    • Run Commands on Files Based on Git Status
  • Go
    • Intro to Go
    • Unit testing in Go
    • Go Strings
    • Intro to Go arrays
    • Intro to Go slices
    • Go Error Handling
    • Date and Time in Go
  • Text Editors
    • Emacs
      • Emacs Practical Editing Examples
      • Emacs Org-Mode Tips
    • Vim
      • CoC LSP
      • Search and Replace Until Next UpperCase Char
      • Neovim Telescope Tips
  • Haskell
    • Syntax
    • Haskell Sectioning
    • Haskell Programming From First Principles (book)
      • 04 - Data Types
        • Datatypes
        • Data Types Questions and Answers
      • ch09 Exercises :: Lists
  • Ruby
    • Unit Tests | Ruby
    • RSpec | Ruby
    • Scope | Ruby
    • HackerRank Ruby Tutorial
    • Bundler | Ruby
    • RVM | Ruby
    • Pry | Ruby
  • Ruby on Rails
    • Command Line | Ruby on Rails
    • Ruby on Rails and Docker
    • Unit Tests | Ruby on Rails
    • Models and Migrations | Ruby on Rails
  • PureScript
    • Read, Eval, Print, Loop: REPL
    • Type Signatures
    • Functions
    • Modules
    • Types
      • Char and String Types
      • Data Types
      • Number Types
    • Char and String Types
    • Number Types
    • Data Types
    • Lists
    • PureScript Explained
      • const
      • apply, ‘$’
      • applyFlipped, ‘#’
      • Cons (Data.List)
  • JavaScript
    • Logging
    • Arrays
    • Async Examples with Ramda
    • Do It With Ramda Part I
    • Gotchas, Surprises and Pain Points | JavaScript
    • JavaScript Performance
    • LeetCode 30 Days of JavaScript
  • TypeScript
    • tsconfig.json Examples
    • Structural Typing
    • Any and Unknown Top Types
    • Type Aliases and Interfaces
    • Object Types
    • Union Types
    • Intersection Types
    • keyof
    • Mapped Types
    • Functions | TypeScript
    • infer keyword | TypeScript
    • React Component-Related Types
  • Scheme
    • Building and Installing Chicken Scheme 5.3.0rc3 on macOS Big Sur
  • Deno Runtime
    • Project Root Absolute Imports and Import Maps in Deno
  • How to Design Programs
    • Beginning Student Languages
  • Algorithms
    • Big-O of Arrays and Objects
    • Space Complexity Examples
    • Logarithms
    • Problem-Solving
    • Helper Functions
    • Split Int Into Even Parts
  • Mostly Adequate Guide to Functional Programming
    • 02 - First Class Functions
    • 02 Currying
  • Composable Functional JavaScript
    • 01 - Create linear data flow with container style types (Box)
    • 02 - Refactor Imperative to Composed Expression using Box
    • 03 - Enforce a null check with composable code branching using Either
    • 04 - Use chain for composable error handling with nested Eithers
    • 05 - A collection of Either examples compared to imperative code
    • Helper Functions
  • Codewars Challenges
    • 6kyu
      • Find Int That Appears Odd Number of Times
      • Which Are In
      • Friend or Foe
    • 7kyu
      • Sort Numbers
      • Valid Parentheses
      • Vowel Count
      • Simple Fun: Diplomas
  • HackerRank Challenges
    • Problem Solving
      • Diagonal Difference
      • Birthday Cake Candles
      • Compare The Triplets
      • Grading Students
      • Plus Minus
      • Sherlock and Array
      • Staircase
      • Strong Password
      • Time Conversion
      • Camel Case
      • Ice Cream Parlor
      • Missing Numbers
      • Mini Max Sum
    • Easy HackerRank Challenges
      • A Very Big Sum
      • Grading Students
    • Helper Functions
  • Programming
    • Unnest Logic From Inside Conditions
  • Docker
    • Bridge Network Examples
  • Databases and SQL
    • PSQL CLI :: PostgreSQL
    • Useful PostgreSQL Commands
    • Basic SQL
    • Basic HackerRank SQL Challenges
  • Design Patterns
    • Template Method Pattern
    • Strategy Pattern
  • SOLID Principles
    • Open Closed Principle
    • Liskov Substitution Principle
    • Interface Segregation Principle
    • Dependency Inversion Principle
  • Character Encoding
    • Useful Unicode Symbols
  • Software Engineering
  • Miscellaneous Tips
    • PlantUML on Sphinx/Read The Docs
    • Paste Image Into Finder
Back to top
View this page

React Component-Related Types¶

Note

Remember that the way the type system works in TypeScript depends on tsconfig.json configuration.

Tip

The following examples were written and tested with typescript@5.4.5, @types/react@18.2.75, and this tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "strict": true,
    "checkJs": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "jsx": "react-jsx",
    "forceConsistentCasingInFileNames": true,
    "rootDir": "./src",
    "outDir": "./dist"
  }
}

React.ReactElement¶

Here’s the type for React.ReactElement:

declare namespace React {
  /**
   * Represents a JSX element.
   *
   * Where {@link ReactNode} represents everything that can be
   * rendered, `ReactElement` only represents JSX.
   *
   * @template P The type of the props object
   * @template T The type of the component or tag
   *
   * @example
   *
   * ```tsx
   * const element: ReactElement = <div />;
   * ```
   */
  interface ReactElement<
    P = any,
    T extends
        string 
      | JSXElementConstructor<any> = string
      | JSXElementConstructor<any>,
  > {
    type: T;
    props: P;
    key: string | null;
  }
}

Basically, an interface with two generic type parameter for props and type. It matches an object with the properties type, props, and key. E.g.:

// OK
const elem: ReactElement = {
  type: "p",
  props: { className: "p-default", text: "Hello!" },
  key: "p1",
};

A ReactElement cannot be undefined, null, an array, or anything which is not an object which does not satisfy the expected structure (type, props and key). It does, however, accept an element constructed with JSX syntax:

// OK
const elem: ReactElement = <div />;

Even though <div /> is technically of type JSX.Element (it is even inferred as JSX.Element if no explicit type annotation is provided), it is compatible with ReactElement.

JSX.Element¶

JSX.Element is based off of ReactElement but with the <any, any> generic, which is to allow libraries to implement JSX in ways that suit them.

The type for JSX.Element

declare global {
  namespace JSX {
    interface Element extends React.ReactElement<any, any> {}
  }
}

React.ReactNode¶

The type for ReactNode:

type ReactNode =
    | ReactElement
    | string
    | number
    | Iterable<ReactNode>
    | ReactPortal
    | boolean
    | null
    | undefined;

Note it is very broad. It includes ReactElement, numbers, strings, null, undefined, and Iterable<ReactNode>:

import { type ReactNode } from "react";

var node: ReactNode;
node = undefined;
node = null;
node = [];
node = [null, undefined];
node = <div />;
node = 1;
node = "hello";
node = [
  <div />,
  {
    type: "p",
    props: { className: "p-default", text: "Hello!" },
    key: "p1",
  },
];
node = createElement("h1", null, "Heading");

Values not compatible with ReactNode are objects that do not satisfy the type of ReactElement or arrays of such incompatible objects:

// NOK
import { type ReactNode } from "react";

let node: ReactNode;
node = {};
node = { id: 1 };

// NOK
let nodes: Array<ReactNode>;
nodes = [{}, { name: "Ahsoka Tano "}];

Links and Resources¶

  • https://stackoverflow.com/questions/58123398/when-to-use-jsx-element-vs-reactnode-vs-reactelement

  • https://legacy.reactjs.org/blog/2014/10/14/introducing-react-elements.html

Copyright © 2025 Fernando Basso | Text licensed under CC BY-NC-SA 4.0 and code licensed under GPLv3
| Built with Sphinx and @pradyunsg's Furo theme. | Show Source | Dev How To Gitlab Repository
On this page
  • React Component-Related Types
    • React.ReactElement
    • JSX.Element
    • React.ReactNode
    • Links and Resources