Edit this page
An in-depth guide on configuring an Expo project with TypeScript.
Expo has first-class support for TypeScript. The JavaScript interface of Expo SDK is written in TypeScript.
This guide provides a quick way to get started for a new project and also steps to migrate your existing JavaScript based Expo project to use TypeScript.
To create a new project, use the default template which includes base TypeScript configuration, example code, and basic navigation structure:
-
npx create-expo-app@latest
After you create a new project using the command above, make sure to follow instructions from:
To migrate your existing JavaScript based project to use TypeScript, follow the instructions below:
1
Rename files to convert them to TypeScript. For example, start with the root component file such as App.js and rename it to App.tsx:
-
mv App.js App.tsx
Tip: Use the .tsx extension if the file includes React components (JSX). If the file does not include any JSX, you can use the .ts file extension.
2
To install required devDependencies
such as typescript
and @types/react
in package.json:
# For all other package managers
-
npx expo install -- --save-dev typescript @types/react
# For yarn
-
yarn add -D typescript @types/react
Alternatively, runnpx expo start
command to installtypescript
and@types/react
dev dependencies.
3
A project's tsconfig.json should extend the expo/tsconfig.base
by default. You can automatically generate a tsconfig.json file by running the command:
-
npx expo customize tsconfig.json
The default configuration in tsconfig.json is user-friendly and encourages adoption. If you prefer strict type checking and reduce the chances of runtime errors, enable strict
under compilerOptions
:
{
"compilerOptions": {
"strict": true
}
}
4
Expo CLI supports path aliases in tsconfig.json automatically. It allows importing modules with custom aliases instead of relative paths.
For example, to import Button
component from src/components/Button.tsx using the alias @/components/Button, add the alias @/*
in tsconfig.json and set it to the src directory:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
}
}
tsconfigPaths
is enabled by default which allows you to set path aliases. You can disable it by setting tsconfigPaths
to false
in the project's app config:
{
"expo": {
"experiments": {
"tsconfigPaths": false
}
}
}
When using path aliases, consider the following:
@expo/webpack-config
.5
To enable absolute imports from a project's root directory, define compilerOptions.baseUrl
the tsconfig.json file:
{
"compilerOptions": {
"baseUrl": "./"
}
}
For example, setting the above configuration allows importing Button
component from the path src/components/Button:
import Button from 'src/components/Button';
When using absolute imports, consider the following:
compilerOptions.paths
are resolved relative to the compilerOptions.baseUrl
if it is defined, otherwise they're resolved against the project root directory.compilerOptions.baseUrl
is resolved before node modules. This means if you have a file named ./path.ts
, it can be imported instead of a node module named path
.compilerOptions.baseUrl
after modifying the tsconfig.json.@expo/webpack-config
.Some Expo libraries provide both static types and type generation capabilities. These types are automatically generated when the project builds or by running the npx expo customize tsconfig.json
command.
Additional setup is required to use TypeScript for configuration files such as metro.config.js or app.config.js. You need to utilize ts-node
require hook to import TypeScript files within your JS config file. This hook allows TypeScript imports while keeping the root file as JavaScript.
# For all other package managers
-
npx expo install -- --save-dev ts-node
# For yarn
-
yarn add -D ts-node
Update metro.config.js to require metro.config.ts file:
require('ts-node/register');
module.exports = require('./metro.config.ts');
Update metro.config.ts file with your project's metro configuration:
import { getDefaultConfig } from 'expo/metro-config';
const config = getDefaultConfig(__dirname);
module.exports = config;
Install the @expo/webpack-config
package.
require('ts-node/register');
module.exports = require('./webpack.config.ts');
import createExpoWebpackConfigAsync from '@expo/webpack-config/webpack';
import { Arguments, Environment } from '@expo/webpack-config/webpack/types';
module.exports = async function (env: Environment, argv: Arguments) {
const config = await createExpoWebpackConfigAsync(env, argv);
// Customize the config before returning it.
return config;
};
app.config.ts is supported by default. However, it doesn't support external TypeScript modules, or tsconfig.json customization. You can use the following approach to get a more comprehensive TypeScript setup:
import 'ts-node/register'; // Add this to import TypeScript files
import { ExpoConfig } from 'expo/config';
const config: ExpoConfig = {
name: 'my-app',
slug: 'my-app',
};
export default config;
Some language features may require additional configuration. For example, if you want to use decorators you'll need to add the experimentalDecorators
option. For more information on the available properties see the TypeScript compiler options documentation.
A good place to start learning TypeScript is the official TypeScript Handbook.
For TypeScript and React components, we recommend referring to the React TypeScript CheatSheet to learn how to type your React components in a variety of common situations.