-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
280 additions
and
280 deletions.
There are no files selected for viewing
80 changes: 40 additions & 40 deletions
80
packages/documentation/copy/ja/javascript/Creating DTS files From JS.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,88 @@ | ||
--- | ||
title: Creating .d.ts Files from .js files | ||
title: .jsファイルから.d.tsファイルを生成する | ||
layout: docs | ||
permalink: /docs/handbook/declaration-files/dts-from-js.html | ||
oneline: "How to add d.ts generation to JavaScript projects" | ||
permalink: /ja/docs/handbook/declaration-files/dts-from-js.html | ||
oneline: "JavaScriptプロジェクトでd.tsファイルを生成する方法" | ||
translatable: true | ||
--- | ||
|
||
[With TypeScript 3.7](/docs/handbook/release-notes/typescript-3-7.html#--declaration-and---allowjs), | ||
TypeScript added support for generating .d.ts files from JavaScript using JSDoc syntax. | ||
[TypeScript 3.7](/docs/handbook/release-notes/typescript-3-7.html#--declaration-and---allowjs)では、 | ||
TypeScriptに、JSDoc構文を使ったJavaScriptから.d.tsファイルを生成するためのサポートが導入されました。 | ||
|
||
This set up means you can own the editor experience of TypeScript-powered editors without porting your project to TypeScript, or having to maintain .d.ts files in your codebase. | ||
TypeScript supports most JSDoc tags, you can find [the reference here](/docs/handbook/type-checking-javascript-files.html#supported-jsdoc). | ||
この仕組みは、プロジェクトをTypeScriptに移行することなく、TypeScriptが備わったエディタの経験を自分のものにできるということを意味します。 | ||
TypeScriptはほとんどのJSDocタグをサポートしています。リファレンスは[こちら](/docs/handbook/type-checking-javascript-files.html#supported-jsdoc)。 | ||
|
||
## Setting up your Project to emit .d.ts files | ||
## .d.tsファイルを出力するようにプロジェクトを設定する | ||
|
||
To add creation of .d.ts files in your project, you will need to do up-to four steps: | ||
プロジェクトに.d.tsファイルの作成を追加するように設定するには、最大4つのステップを実行する必要があります: | ||
|
||
- Add TypeScript to your dev dependencies | ||
- Add a `tsconfig.json` to configure TypeScript | ||
- Run the TypeScript compiler to generate the corresponding d.ts files for JS files | ||
- (optional) Edit your package.json to reference the types | ||
- dev dependenciesにTypeScriptを追加する | ||
- TypeScriptを設定するための`tsconfig.json`を追加する | ||
- TypeScriptコンパイラを実行して、JSファイルに対応するd.tsファイルを生成する | ||
- (オプション) package.jsonを編集して型を参照できるようにする | ||
|
||
### Adding TypeScript | ||
### TypeScriptを追加する | ||
|
||
You can learn how to do this in our [installation page](/download). | ||
こちらは、[インストールページ](/download)を参照してください。 | ||
|
||
### TSConfig | ||
|
||
The TSConfig is a jsonc file which configures both your compiler flags, and declare where to find files. | ||
In this case, you will want a file like the following: | ||
TSConfigはコンパイラのフラグを設定し、対象のファイルを宣言するためのjsoncファイルです。 | ||
今回のケースでは、次のようなファイルが必要になるでしょう: | ||
|
||
```json5 | ||
{ | ||
// Change this to match your project | ||
// プロジェクトに合わせて変更してください | ||
include: ["src/**/*"], | ||
|
||
compilerOptions: { | ||
// Tells TypeScript to read JS files, as | ||
// normally they are ignored as source files | ||
// JSファイルは通常はソースファイルとしては無視されますが、 | ||
// ここではJSファイルを読み込むようにTypeScriptに指示します | ||
allowJs: true, | ||
// Generate d.ts files | ||
// d.tsファイルを生成します | ||
declaration: true, | ||
// This compiler run should | ||
// only output d.ts files | ||
// コンパイラを実行すると | ||
// d.tsファイルのみ出力されます | ||
emitDeclarationOnly: true, | ||
// Types should go into this directory. | ||
// Removing this would place the .d.ts files | ||
// next to the .js files | ||
// 型はこのディレクトリに出力されます | ||
// このオプションを削除すると | ||
// .jsファイルの隣に.d.tsファイルが置かれます | ||
outDir: "dist", | ||
}, | ||
} | ||
``` | ||
|
||
You can learn more about the options in the [tsconfig reference](/reference). | ||
An alternative to using a TSConfig file is the CLI, this is the same behavior as a CLI command. | ||
オプションについては、[tsconfigリファレンス](/reference)で詳しく知ることができます。 | ||
TSConfigファイルを使用する代替手段としてCLIがあります。次は上記のTSConfigファイルと同じふるまいをするCLIコマンドです。 | ||
|
||
```sh | ||
npx typescript src/**/*.js --declaration --allowJs --emitDeclarationOnly --outDir types | ||
``` | ||
|
||
## Run the compiler | ||
## コンパイラを実行する | ||
|
||
You can learn how to do this in our [installation page](/download). | ||
You want to make sure these files are included in your package if you have the files in your project's `.gitignore`. | ||
実行方法については[インストールページ](/download)を参照してください。 | ||
プロジェクトの`.gitignore`に生成されたファイルがある場合は、確実にパッケージにそれらのファイルが含まれるようにしてください。 | ||
|
||
## Editing the package.json | ||
## package.jsonを編集する | ||
|
||
TypeScript replicates the node resolution for modules in a `package.json`, with an additional step for finding .d.ts files. | ||
Roughly, the resolution will first check the optional `"types"` field, then the `"main"` field, and finally will try `index.d.ts` in the root. | ||
TypeScriptは、`package.json`の中でNodeのモジュール解決を再現し、.d.tsファイルを見つけるためのステップを追加します。 | ||
大まかには、ノード解決はオプションである`"types"`フィールドをチェックし、次に`"main"`フィールド、そして最後にルートの`index.d.ts`を試します。 | ||
|
||
| Package.json | Location of default .d.ts | | ||
| Package.json | デフォルトの.d.tsの場所 | | ||
| :------------------------ | :----------------------------- | | ||
| No "types" field | checks "main", then index.d.ts | | ||
| "types"フィールドがない | "main"をチェックし、次にindex.d.ts| | ||
| "types": "main.d.ts" | main.d.ts | | ||
| "types": "./dist/main.js" | ./main/main.d.ts | | ||
|
||
If absent, then "main" is used | ||
もし存在しなければ、次は"main"が使用されます | ||
|
||
| Package.json | Location of default .d.ts | | ||
| Package.json | デフォルトの.d.tsの場所 | | ||
| :----------------------- | :------------------------ | | ||
| No "main" field | index.d.ts | | ||
| "main"フィールドがない | index.d.ts | | ||
| "main":"index.js" | index.d.ts | | ||
| "main":"./dist/index.js" | ./dist/index.d.ts | | ||
|
||
## Tips | ||
|
||
If you'd like to write tests for your .d.ts files, try [tsd](https://github.com/SamVerschueren/tsd). | ||
.d.tsファイルにテストを記述したいなら、[tsd](https://github.com/SamVerschueren/tsd)を試してみてください。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.