Skip to main content

Overview

TypeScript provides type safety, better IDE support, and improved code quality. Expo has first-class TypeScript support with automatic configuration.

Setup

New projects

Create an Expo app with TypeScript:

Existing projects

1

Rename a file to .tsx

2

Start the dev server

Expo automatically creates tsconfig.json and installs TypeScript dependencies.
3

Install type definitions

TypeScript Configuration

Basic tsconfig.json

Expo generates this automatically:
tsconfig.json

Strict mode configuration

For better type safety:
tsconfig.json

Path aliases

tsconfig.json
Usage:

Component Types

Functional components

components/Button.tsx

With children

components/Card.tsx

Generic components

components/List.tsx
Usage:

Hooks with TypeScript

useState

useRef

useEffect

Custom hooks

hooks/useApi.ts
Usage:

Typed Routes with Expo Router

Generate types

Expo Router automatically generates route types:

Using typed routes

app/index.tsx

Route parameters

app/posts/[id].tsx

API Types

Defining API responses

types/api.ts

Type-safe API client

utils/api.ts

Type Guards

utils/guards.ts
Usage:

Utility Types

Commonly used utility types

types/utils.ts

Custom utility types

types/utils.ts

Type-safe Context

contexts/AuthContext.tsx

Environment Variables

env.d.ts
Usage:

Type Errors and Debugging

Common errors

Best Practices

  • Enable strict mode: Use "strict": true in tsconfig.json
  • Avoid any: Use unknown instead and type guard
  • Use type inference: Let TypeScript infer types when obvious
  • Define API types: Create types for all API responses
  • Use utility types: Leverage built-in utility types
  • Type guards: Create type guards for runtime checks
  • Readonly where appropriate: Use readonly for immutable data
  • Const assertions: Use as const for literal types
  • Named types: Use type or interface instead of inline types
  • Export types: Make types reusable across files