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
TypeScript Configuration
Basic tsconfig.json
Expo generates this automatically:tsconfig.json
Strict mode configuration
For better type safety:tsconfig.json
Path aliases
tsconfig.json
Component Types
Functional components
components/Button.tsx
With children
components/Card.tsx
Generic components
components/List.tsx
Hooks with TypeScript
useState
useRef
useEffect
Custom hooks
hooks/useApi.ts
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
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
Type Errors and Debugging
Common errors
'X' is possibly 'null' or 'undefined'
'X' is possibly 'null' or 'undefined'
Type 'X' is not assignable to type 'Y'
Type 'X' is not assignable to type 'Y'
Property 'X' does not exist on type 'Y'
Property 'X' does not exist on type 'Y'
Best Practices
- Enable strict mode: Use
"strict": truein tsconfig.json - Avoid
any: Useunknowninstead 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
readonlyfor immutable data - Const assertions: Use
as constfor literal types - Named types: Use
typeorinterfaceinstead of inline types - Export types: Make types reusable across files