使用 TypeScript

编辑页面

关于为 Expo 项目配置 TypeScript 的深入指南。


For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.

Expo 对 TypeScript 提供一流支持。Expo SDK 的 JavaScript 接口是用 TypeScript 编写的。

本指南提供了一种快速开始新项目的方法,也提供了将现有基于 JavaScript 的 Expo 项目迁移为使用 TypeScript 的步骤。

快速开始

要创建新项目,请使用默认模板,其中包含基础 TypeScript 配置、示例代码和基础导航结构:

Terminal
npx create-expo-app@latest --template default@sdk-55

在使用上述命令创建新项目后,请务必按照以下内容中的说明进行操作:

  • 设置你的环境,其中提供了搭建本地开发环境所需的步骤。
  • 开始开发,其中提供了关于启动开发服务器、文件结构以及其他功能的详细信息。

迁移现有 JavaScript 项目

要将现有基于 JavaScript 的项目迁移为使用 TypeScript,请按照以下说明操作:

1

将文件重命名为 .tsx 或 .ts 扩展名

将文件重命名以转换为 TypeScript。例如,从根组件文件开始,例如 App.js,将其重命名为 App.tsx

Terminal
mv App.js App.tsx
提示: 如果文件包含 React 组件(JSX),请使用 .tsx 扩展名。如果文件不包含任何 JSX,则可以使用 .ts 文件扩展名。

2

安装所需的开发依赖

要在 package.json 中安装所需的 devDependencies,例如 typescript@types/react

Terminal
npx expo install typescript @types/react --dev
Terminal
npx expo install typescript @types/react "--" --dev
或者,运行 npx expo start 命令来安装 typescript@types/react 的开发依赖。
使用 tsc 对项目文件进行类型检查

要对项目文件进行类型检查,请在项目目录根目录中运行 tsc 命令:

Terminal
# 对于 npm
npm run tsc

# 对于 yarn
yarn tsc

3

使用 tsconfig.json 添加基础配置

项目的 tsconfig.json 默认应继承 expo/tsconfig.base。你可以通过运行以下命令自动生成 tsconfig.json 文件:

Terminal
npx expo customize tsconfig.json

tsconfig.json 中的默认配置对用户友好,并鼓励采用。如果你更喜欢 严格类型检查 并减少运行时错误的可能性,可以在 compilerOptions 下启用 strict

tsconfig.json
{ "extends": "expo/tsconfig.base", "compilerOptions": { "strict": true } }

4

路径别名(可选)

Expo CLI 会自动支持 tsconfig.json 中的 路径别名。它允许使用自定义别名来导入模块,而不是使用相对路径。

例如,要使用别名 @/components/Buttonsrc/components/Button.tsx 导入 Button 组件,请在 tsconfig.json 中添加别名 @/* 并将其设置为 src 目录:

tsconfig.json
{ "extends": "expo/tsconfig.base", "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["src/*"] } } }
禁用路径别名

tsconfigPaths 默认启用,这使你可以设置路径别名。你可以在项目的 app 配置 中将 tsconfigPaths 设为 false 来禁用它:

app.json
{ "expo": { "experiments": { "tsconfigPaths": false } } }

注意事项

使用路径别名时,请考虑以下内容:

  • 修改 tsconfig.json 后重启 Expo CLI 以更新路径别名。别名变更时无需清除 Metro 缓存。
  • 如果不使用 TypeScript,jsconfig.json 可以作为 tsconfig.json 的替代方案。
  • 定义路径别名会增加额外的解析时间。
  • 路径别名仅被 Metro(包括 Metro web)支持,不被已弃用的 @expo/webpack-config 支持。
  • 裸工作流项目需要为此功能进行额外设置。更多信息请参见 Metro 设置指南

5

绝对导入(可选)

要从项目根目录启用绝对导入,请在 tsconfig.json 文件中定义 compilerOptions.baseUrl

tsconfig.json
{ "extends": "expo/tsconfig.base", "compilerOptions": { "baseUrl": "./" } }

例如,设置上述配置后,可以从路径 src/components/Button 导入 Button 组件:

import Button from 'src/components/Button';

注意事项

使用绝对导入时,请考虑以下内容:

  • 如果定义了 compilerOptions.baseUrl,则 compilerOptions.paths 会相对于它进行解析,否则将相对于项目根目录进行解析。
  • compilerOptions.baseUrl 的解析优先于 node modules。这意味着如果你有一个名为 ./path.ts 的文件,它可能会被导入而不是名为 path 的 node module。
  • 修改 tsconfig.json 后,需要重启 Expo CLI 以更新 compilerOptions.baseUrl
  • 如果你不使用 TypeScript,jsconfig.json 可以作为 tsconfig.json 的替代方案。
  • 绝对导入仅被 Metro(包括 Metro web)支持,不被 @expo/webpack-config 支持。
  • 裸工作流项目需要为此功能进行额外设置。更多信息请参见 版本化的 Metro 设置指南

类型生成

某些 Expo 库同时提供静态类型和类型生成能力。这些类型会在项目构建时自动生成,或者通过运行 npx expo customize tsconfig.json 命令生成。

用于项目配置文件的 TypeScript

要在 metro.config.jsapp.config.js 等配置文件中使用 TypeScript,需要额外设置。

安装 tsx 作为开发依赖,并利用其 tsx/cjs require hook 在你的 JS 配置文件中导入 TypeScript 文件。这个 hook 允许在保持根文件为 JavaScript 的同时使用 TypeScript 导入。下面的命令会添加 tsx,这样在以下子章节示例中 import 'tsx/cjs' 就能工作。

Terminal
npx expo install tsx --dev
Terminal
npx expo install tsx "--" --dev

metro.config.js

更新 metro.config.js,使其引用 metro.config.ts 文件:

metro.config.js
require('tsx/cjs'); // 添加这一行以导入 TypeScript 文件 module.exports = require('./metro.config.ts');

使用项目的 Metro 配置更新 metro.config.ts 文件:

metro.config.ts
import { getDefaultConfig } from 'expo/metro-config'; const config = getDefaultConfig(__dirname); module.exports = config;
已弃用:webpack.config.js

安装 @expo/webpack-config 包。

webpack.config.js
require('tsx/cjs'); // 添加这一行以导入 TypeScript 文件 module.exports = require('./webpack.config.ts');
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); // 在返回之前自定义配置。 return config; };

app.config.js

默认支持 app.config.ts。但是,它不支持外部 TypeScript 模块,也不支持 tsconfig.json 自定义。你可以使用以下方法获得更完整的 TypeScript 配置:

app.config.ts
import 'tsx/cjs'; // 添加这一行以导入 TypeScript 文件 import { ExpoConfig } from 'expo/config'; const config: ExpoConfig = { name: 'my-app', slug: 'my-app', }; export default config;

其他 TypeScript 功能

某些语言特性可能需要额外配置。例如,如果你想使用装饰器,就需要添加 experimentalDecorators 选项。有关可用属性的更多信息,请参阅 TypeScript 编译器选项 文档。

了解如何使用 TypeScript

学习 TypeScript 的一个好起点是官方的 TypeScript Handbook

对于 TypeScript 和 React 组件, 我们建议参考 React TypeScript CheatSheet,以了解如何在各种常见场景下为你的 React 组件添加类型。