TypeScript
Next.js 内置了 TypeScript 支持,当你使用 create-next-app
创建新项目时,会自动安装必要的包并配置适当的设置。
要将 TypeScript 添加到现有项目中,只需将文件重命名为 .ts
/ .tsx
。运行 next dev
和 next build
会自动安装必要的依赖项,并添加一个带有推荐配置选项的 tsconfig.json
文件。
值得注意的是:如果你已经有一个
jsconfig.json
文件,请将旧jsconfig.json
中的paths
编译器选项复制到新的tsconfig.json
文件中,然后删除旧的jsconfig.json
文件。
示例
类型检查 next.config.ts
你可以在 Next.js 配置中使用 TypeScript 并导入类型,方法是使用 next.config.ts
。
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
/* 这里是配置选项 */
}
export default nextConfig
值得注意的是:目前
next.config.ts
中的模块解析仅限于CommonJS
。这可能会导致与在next.config.ts
中加载的仅 ESM 包不兼容的问题。
当使用 next.config.js
文件时,你可以使用 JSDoc 在 IDE 中添加一些类型检查,如下所示:
// @ts-check
/** @type {import('next').NextConfig} */
const nextConfig = {
/* 这里是配置选项 */
}
module.exports = nextConfig
静态生成和服务器端渲染
对于 getStaticProps
、getStaticPaths
和 getServerSideProps
,你可以分别使用 GetStaticProps
、GetStaticPaths
和 GetServerSideProps
类型:
import type { GetStaticProps, GetStaticPaths, GetServerSideProps } from 'next'
export const getStaticProps = (async (context) => {
// ...
}) satisfies GetStaticProps
export const getStaticPaths = (async () => {
// ...
}) satisfies GetStaticPaths
export const getServerSideProps = (async (context) => {
// ...
}) satisfies GetServerSideProps
值得注意的是:
satisfies
是在 TypeScript 4.9 中添加的。我们建议升级到最新版本的 TypeScript。
与 API 路由
以下是如何在 API 路由中使用内置类型的示例:
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) {
res.status(200).json({ name: 'John Doe' })
}
你还可以为响应数据添加类型:
import type { NextApiRequest, NextApiResponse } from 'next'
type Data = {
name: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
res.status(200).json({ name: 'John Doe' })
}
与自定义 App
如果你有一个自定义 App
,你可以使用内置类型 AppProps
并将文件名更改为 ./pages/_app.tsx
,如下所示:
import type { AppProps } from 'next/app'
export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
增量类型检查
从 v10.2.1
开始,Next.js 支持增量类型检查,当在你的 tsconfig.json
中启用时,这可以帮助加速大型应用程序中的类型检查。
在生产环境中禁用 TypeScript 错误
当你的项目中存在 TypeScript 错误时,Next.js 会使你的生产构建(next build
)失败。
如果你希望 Next.js 在应用程序有错误时仍然危险地生成生产代码,你可以禁用内置类型检查步骤。
如果禁用了类型检查,请确保你在构建或部署过程中运行类型检查,否则这可能非常危险。
打开 next.config.ts
并在 typescript
配置中启用 ignoreBuildErrors
选项:
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
typescript: {
// !! 警告 !!
// 危险地允许生产构建成功完成,即使
// 你的项目有类型错误。
// !! 警告 !!
ignoreBuildErrors: true,
},
}
export default nextConfig
值得注意的是:你可以在构建前运行
tsc --noEmit
来自己检查 TypeScript 错误。这对于在部署前检查 TypeScript 错误的 CI/CD 管道很有用。
自定义类型声明
当你需要声明自定义类型时,你可能会想要修改 next-env.d.ts
。但是,这个文件是自动生成的,所以你做的任何更改都会被覆盖。相反,你应该创建一个新文件,比如叫 new-types.d.ts
,并在你的 tsconfig.json
中引用它:
{
"compilerOptions": {
"skipLibCheck": true
//...省略...
},
"include": [
"new-types.d.ts",
"next-env.d.ts",
".next/types/**/*.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": ["node_modules"]
}
版本变更
版本 | 变更 |
---|---|
v15.0.0 | 为 TypeScript 项目添加了 next.config.ts 支持。 |
v13.2.0 | 静态类型链接处于测试阶段。 |
v12.0.0 | 现在默认使用 SWC 来编译 TypeScript 和 TSX,以加快构建速度。 |
v10.2.1 | 当在你的 tsconfig.json 中启用时,添加了增量类型检查支持。 |