Skip to content

Commit 0f73d64

Browse files
committed
update: typescript.md
1 parent 46d567f commit 0f73d64

File tree

1 file changed

+95
-27
lines changed

1 file changed

+95
-27
lines changed

docs/basic-features/typescript.md

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,57 @@ description: Next.jsは標準でTypeScriptをサポートし、ページとAPI
55
# TypeScript
66

77
<details>
8-
<summary><b>例</b></summary>
9-
<ul>
10-
<li><a href="https://github.com/vercel/next.js/tree/canary/examples/with-typescript">TypeScript</a></li>
11-
</ul>
8+
<summary><b>Version History</b></summary>
9+
10+
| Version | Changes |
11+
| --------- | ------------------------------------------------------------------------------------------------------------------------------------ |
12+
| `v12.0.0` | [SWC](https://nextjs.org/docs/advanced-features/compiler) is now used by default to compile TypeScript and TSX for faster builds. |
13+
| `v10.2.1` | [Incremental type checking](https://www.typescriptlang.org/tsconfig#incremental) support added when enabled in your `tsconfig.json`. |
14+
1215
</details>
1316

1417
Next.js は、IDE のように統合された革新的な[TypeScript](https://www.typescriptlang.org/)体験を提供します。
1518

16-
はじめに、プロジェクトのルートに空の `tsconfig.json` ファイルを作成します:
19+
Next.js provides an integrated [TypeScript](https://www.typescriptlang.org/) experience, including zero-configuration set up and built-in types for Pages, APIs, and more.
20+
21+
- [Clone and deploy the TypeScript starter](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnext.js%2Ftree%2Fcanary%2Fexamples%2Fwith-typescript&project-name=with-typescript&repository-name=with-typescript)
22+
- [View an example application](https://github.com/vercel/next.js/tree/canary/examples/with-typescript)
23+
24+
## `create-next-app` support
25+
26+
You can create a TypeScript project with [`create-next-app`](https://nextjs.org/docs/api-reference/create-next-app) using the `--ts, --typescript` flag like so:
27+
28+
```
29+
npx create-next-app@latest --ts
30+
# or
31+
yarn create next-app --typescript
32+
```
33+
34+
## Existing projects
35+
36+
To get started in an existing project, create an empty `tsconfig.json` file in
37+
the root folder:
1738

1839
```bash
1940
touch tsconfig.json
2041
```
2142

2243
Next.js はこのファイルをデフォルト値で自動的に構成します。独自の `tsconfig.json` カスタム[コンパイラオプション](https://www.typescriptlang.org/docs/handbook/compiler-options.html)を提供することもサポートされています。
2344

24-
> Next.jsはTypeScriptを処理するためにBabelを用いますが、いくつかの[注意点](https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats)があり、一部の[コンパイラオプションの挙動が異なります](https://babeljs.io/docs/en/babel-plugin-transform-typescript#typescript-compiler-options)
45+
You can also provide a relative path to a tsconfig.json file by setting `typescript.tsconfigPath` prop inside your `next.config.js` file.
46+
47+
Starting in `v12.0.0`, Next.js uses [SWC](https://nextjs.org/docs/advanced-features/compiler) by default to compile TypeScript and TSX for faster builds.
48+
49+
> Next.js は `.babelrc` が存在する場合、 TypeScript を処理するために Babel を用いますが、これはいくつかの[注意点](https://babeljs.io/docs/en/babel-plugin-transform-typescript#caveats)があり、一部の[コンパイラオプションの挙動が異なります](https://babeljs.io/docs/en/babel-plugin-transform-typescript#typescript-compiler-options)
2550
26-
次に、`next`(通常は `npm run dev`)を実行すると、セットアップのために Next.js が必要なパッケージのインストールを案内します:
51+
次に、`next`(通常は `npm run dev` or `yarn dev`)を実行すると、セットアップのために Next.js が必要なパッケージのインストールを案内します:
2752

2853
```bash
2954
npm run dev
3055

3156
# 次のような指示が表示されます:
3257
#
33-
# Please install typescript, @types/react, and @types/node by running:
58+
# Please install TypeScript, @types/react, and @types/node by running:
3459
#
3560
# yarn add --dev typescript @types/react @types/node
3661
#
@@ -39,12 +64,13 @@ npm run dev
3964

4065
これで、ファイルを `.js` から `.tsx` に変換し、TypeScript の利点を活用する準備が整いました!
4166

42-
> `next-env.d.ts`という名前のファイルがプロジェクトのルートに作成されます。
43-
> このファイルにより、TypeScriptコンパイラによってNext.jsの型が確実に取得されます。このファイルを**削除することはできません**が、編集することはできます(ただし、必須ではありません)。
67+
> `next-env.d.ts`という名前のファイルがプロジェクトのルートに作成されます。このファイルにより、TypeScriptコンパイラによってNext.jsの型が確実に取得されます。このファイルをいつでも変更される可能性があるため、**削除または編集することができません**
4468
45-
> Next.jsでは `strict` モードはデフォルトでオフになっています。TypeScriptに慣れてきたら `tsconfig.json` でオンにすることをお勧めします。
69+
> TypeScript の `strict` モードはデフォルトでオフになっています。TypeScriptに慣れてきたら `tsconfig.json` でオンにすることをお勧めします。
4670
47-
デフォルトでは、Next.js はアクティブに開発に取り組んでいるページに対して TypeScript のエラーを報告します。アクティブでないページに対する TypeScript のエラーは開発工程を邪魔**しません**
71+
By default, Next.js will do type checking as part of `next build`. We recommend using code editor type checking during development.
72+
73+
> Instead of editing `next-env.d.ts`, you can include additional types by adding a new file e.g. `additional.d.ts` and then referencing it in the [`include`](https://www.typescriptlang.org/tsconfig#include) array in your `tsconfig.json`.
4874
4975
エラー報告を静かにさせたい場合は、[TypeScriptのエラーを無視する](/docs/api-reference/next.config.js/ignoring-typescript-errors.md)ためのドキュメントを参照してください。
5076

@@ -56,27 +82,27 @@ npm run dev
5682
```ts
5783
import { GetStaticProps, GetStaticPaths, GetServerSideProps } from 'next';
5884

59-
export const getStaticProps: GetStaticProps = async context => {
85+
export const getStaticProps: GetStaticProps = async (context) => {
6086
// ...
6187
};
6288

6389
export const getStaticPaths: GetStaticPaths = async () => {
6490
// ...
6591
};
6692

67-
export const getServerSideProps: GetServerSideProps = async context => {
93+
export const getServerSideProps: GetServerSideProps = async (context) => {
6894
// ...
6995
};
7096
```
7197

72-
> `getInitialProps`を使用している場合は、[このページの指示に従って](/docs/api-reference/data-fetching/getInitialProps.md#typescript)ください。
98+
> `getInitialProps`を使用している場合は、[このページの指示に従って](/docs/api-reference/data-fetching/get-initial-props.md#typescript)ください。
7399
74100
## APIのルート
75101

76102
以下は、API ルートにビルトインの型を使用する方法の例です:
77103

78104
```ts
79-
import { NextApiRequest, NextApiResponse } from 'next';
105+
import type { NextApiRequest, NextApiResponse } from 'next';
80106

81107
export default (req: NextApiRequest, res: NextApiResponse) => {
82108
res.status(200).json({ name: 'John Doe' });
@@ -86,29 +112,27 @@ export default (req: NextApiRequest, res: NextApiResponse) => {
86112
レスポンスデータにも型を指定できます:
87113

88114
```ts
89-
import { NextApiRequest, NextApiResponse } from 'next';
115+
import type { NextApiRequest, NextApiResponse } from 'next'
90116

91117
type Data = {
92-
name: string;
93-
};
118+
name: string
119+
}
94120

95121
export default (req: NextApiRequest, res: NextApiResponse<Data>) => {
96-
res.status(200).json({ name: 'John Doe' });
97-
};
122+
res.status(200).json({ name: 'John Doe' })
123+
}
98124
```
99125

100126
## カスタム `App`
101127

102-
[カスタム `App` ](/docs/advanced-features/custom-app)がある場合は、ビルトインの `AppProps` 型を使用して、ファイル名を `./pages/_app.tsx` に変更できます:
128+
[カスタム `App` ](/docs/advanced-features/custom-app.md)がある場合は、ビルトインの `AppProps` 型を使用して、ファイル名を `./pages/_app.tsx` に変更できます:
103129

104130
```ts
105-
import { AppProps } from 'next/app';
131+
import type { AppProps } from 'next/app'
106132

107-
function MyApp({ Component, pageProps }: AppProps) {
108-
return <Component {...pageProps} />;
133+
export default function MyApp({ Component, pageProps }: AppProps) {
134+
return <Component {...pageProps} />
109135
}
110-
111-
export default MyApp;
112136
```
113137

114138
## PathのエイリアスとbaseUrl
@@ -117,3 +141,47 @@ Next.js は、`tsconfig.json`の `"paths"` および `"baseUrl"` オプション
117141

118142
この機能の詳細については、[モジュールパスエイリアスのドキュメント](/docs/advanced-features/module-path-aliases.md)をご覧ください。
119143

144+
## Type checking next.config.js
145+
146+
The `next.config.js` file must be a JavaScript file as it does not get parsed by Babel or TypeScript, however you can add some type checking in your IDE using JSDoc as below:
147+
148+
```js
149+
// @ts-check
150+
151+
/**
152+
* @type {import('next').NextConfig}
153+
**/
154+
const nextConfig = {
155+
/* config options here */
156+
}
157+
158+
module.exports = nextConfig
159+
```
160+
161+
## Incremental type checking
162+
163+
Since `v10.2.1` Next.js supports [incremental type checking](https://www.typescriptlang.org/tsconfig#incremental) when enabled in your `tsconfig.json`, this can help speed up type checking in larger applications.
164+
165+
It is highly recommended to be on at least `v4.3.2` of TypeScript to experience the [best performance](https://devblogs.microsoft.com/typescript/announcing-typescript-4-3/#lazier-incremental) when leveraging this feature.
166+
167+
## Ignoring TypeScript Errors
168+
169+
Next.js fails your **production build** (`next build`) when TypeScript errors are present in your project.
170+
171+
If you'd like Next.js to dangerously produce production code even when your application has errors, you can disable the built-in type checking step.
172+
173+
If disabled, be sure you are running type checks as part of your build or deploy process, otherwise this can be very dangerous.
174+
175+
Open `next.config.js` and enable the `ignoreBuildErrors` option in the `typescript` config:
176+
177+
```js
178+
module.exports = {
179+
typescript: {
180+
// !! WARN !!
181+
// Dangerously allow production builds to successfully complete even if
182+
// your project has type errors.
183+
// !! WARN !!
184+
ignoreBuildErrors: true,
185+
},
186+
}
187+
```

0 commit comments

Comments
 (0)