Skip to main content
Theme UI is written in TypeScript and provides comprehensive type definitions. This guide covers TypeScript setup and advanced type customization.

Requirements

Theme UI v0.16+ requires TypeScript 5.1.2 or newer and @types/react published after June 1, 2023.
This requirement exists due to breaking changes in JSX types. See GitHub issue #2430 for details.

Basic Setup

tsconfig.json

Configure your TypeScript compiler for Theme UI:
The JSX Automatic Runtime (react-jsx) is highly encouraged to minimize friction and avoid type errors.

File-level JSX Import Source

Override the default JSX import source per file:

Type Definitions

SxProp Type

The SxProp interface adds the sx prop to components:

ThemeUIStyleObject

The main type for style objects:

Theme Type

The complete theme interface:

Extending Theme Types

Customize the theme type to add autocomplete for your specific theme structure.

Method 1: Module Augmentation

Extend the global theme type:
Now TypeScript will autocomplete your custom theme values:

Method 2: Strict Theme Type

Create a strictly typed theme from scratch:

Component Types

Adding sx to Custom Components

Box Component Props

Themed Component Type

Type definition for Themed components from @theme-ui/mdx:

Responsive Style Values

TypeScript supports responsive arrays:

Theme-aware Function Types

Style values can be functions that receive the theme:

Utility Types

Scale Types

Nested Scales with __default

Common Type Errors

Error: Property ‘sx’ does not exist

Ensure @jsxImportSource theme-ui is at the top of your file:

Error: Type ‘string’ is not assignable to type ‘never’

This happens when TypeScript can’t infer theme types. Add explicit types:

Error: Index signature is missing

When accessing theme values dynamically:

Advanced Patterns

Discriminated Union for Variants

Generic Themed Component

Const Assertion for Strict Typing

Migration from JavaScript

Converting a JavaScript theme to TypeScript:

Resources

TypeScript Handbook

Official TypeScript documentation

Emotion TypeScript

TypeScript guide for Emotion

Theme UI GitHub

Source code and type definitions

Migration Guide

Upgrading to latest version