Skip to main content
Preact has first-class TypeScript support with complete type definitions included in the core package. No additional @types packages are needed.

Setup

Preact’s TypeScript definitions are included by default. Just install Preact and start using TypeScript:

Type Definitions

Preact’s type definitions are located in the source code:
  • src/index.d.ts - Core Preact types
  • src/jsx.d.ts - JSX type definitions
  • hooks/src/index.d.ts - Hooks type definitions
  • compat/src/index.d.ts - React compatibility types
Reference: src/index.d.ts, hooks/src/index.d.ts

Component Types

Function components are typed using the FunctionComponent type:
Or use the shorthand (recommended):

Props with Children

The ComponentChildren type represents valid children:
Alternatively, use FunctionComponent which includes children automatically:

Hooks with TypeScript

Event Handlers

Preact provides specific event types for all DOM events:
Common event types:
  • JSX.TargetedEvent<T, E> - Generic event
  • JSX.TargetedMouseEvent<T> - Mouse events
  • JSX.TargetedKeyboardEvent<T> - Keyboard events
  • JSX.TargetedFocusEvent<T> - Focus events

Refs with TypeScript

Context with TypeScript

Custom Hooks

Type your custom hooks like regular functions:

Generic Components

Create reusable components that work with any type:

Utility Types

Preact exports useful utility types:

Best Practices

Next Steps

Hooks

Learn about hooks with full TypeScript support

Context

Type-safe context with TypeScript