Skip to content

Commit 76d9487

Browse files
doc: 03-typescriptの翻訳
1 parent 5145bc2 commit 76d9487

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

apps/svelte.dev/content/docs/svelte/07-misc/03-typescript.md

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ title: TypeScript
99
- using `Component` and the other helper types
1010
- using `svelte-check` -->
1111

12-
You can use TypeScript within Svelte components. IDE extensions like the [Svelte VS Code extension](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode) will help you catch errors right in your editor, and [`svelte-check`](https://www.npmjs.com/package/svelte-check) does the same on the command line, which you can integrate into your CI.
12+
Svelte コンポーネント内で TypeScript を使えます。[Svelte VS Code 拡張機能](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode)のようなIDE拡張機能はエディタ上でエラーをキャッチするのに役立ちます。また、 [`svelte-check`](https://www.npmjs.com/package/svelte-check) はコマンドラインで同じことができ、CIに統合できます。
1313

1414
## `<script lang="ts">`
1515

16-
To use TypeScript inside your Svelte components, add `lang="ts"` to your `script` tags:
16+
Svelte コンポーネント内で TypeScript を使うには、 `script` タグに `lang="ts"` を追加します。
1717

1818
```svelte
1919
<script lang="ts">
@@ -29,34 +29,34 @@ To use TypeScript inside your Svelte components, add `lang="ts"` to your `script
2929
</button>
3030
```
3131

32-
Doing so allows you to use TypeScript's _type-only_ features. That is, all features that just disappear when transpiling to JavaScript, such as type annotations or interface declarations. Features that require the TypeScript compiler to output actual code are not supported. This includes:
32+
これにより、 TypeScript の機能のうち _型のみ_ 使えます。つまり、型アノテーションやインターフェース宣言など、 JavaScript にトランスコンパイルすると消えてしまうすべての機能です。 TypeScript コンパイラが実際のコードを出力する必要のある機能はサポートされていません。これには以下が含まれます:
3333

34-
- using enums
35-
- using `private`, `protected` or `public` modifiers in constructor functions together with initializers
36-
- using features that are not yet part of the ECMAScript standard (i.e. not level 4 in the TC39 process) and therefore not implemented yet within Acorn, the parser we use for parsing JavaScript
34+
- enumの使用
35+
- コンストラクタ関数で `private` `protected` `public` 修飾子を初期化子(initializer)といっしょに使用する
36+
- ECMAScript 標準の一部ではない(つまりTC39プロセスでレベル4ではない)ため、 JavaScript 解析に用いるパーサー Acorn 内に実装されていない機能の使用
3737

38-
If you want to use one of these features, you need to setup up a `script` preprocessor.
38+
これらいずれかの機能を使いたいときは、 `script` プリプロセッサをセットアップする必要があります。
3939

40-
## Preprocessor setup
40+
## プリプロセッサのセットアップ<!--Preprocessor-setup-->
4141

42-
To use non-type-only TypeScript features within Svelte components, you need to add a preprocessor that will turn TypeScript into JavaScript.
42+
Svelte コンポーネント内で型以外の TypeScript 機能を使うには、 TypeScript JavaScript に変換するプリプロセッサを追加する必要があります。
4343

4444
```ts
4545
/// file: svelte.config.js
4646
// @noErrors
4747
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
4848

4949
const config = {
50-
// Note the additional `{ script: true }`
50+
// `{ script: true }` の追加に注目してください
5151
preprocess: vitePreprocess({ script: true })
5252
};
5353

5454
export default config;
5555
```
5656

57-
### Using SvelteKit or Vite
57+
### SvelteKit または Vite の使用 <!--Using-SvelteKit-or-Vite-->
5858

59-
The easiest way to get started is scaffolding a new SvelteKit project by typing `npx sv create`, following the prompts and choosing the TypeScript option.
59+
もっとも簡単な TypeScript の始め方は、 `npx sv create` と入力してプロンプトに従い、TypeScriptオプションを選択して新しい SvelteKit プロジェクトの骨組みを自動生成することです。
6060

6161
```ts
6262
/// file: svelte.config.js
@@ -70,27 +70,27 @@ const config = {
7070
export default config;
7171
```
7272

73-
If you don't need or want all the features SvelteKit has to offer, you can scaffold a Svelte-flavoured Vite project instead by typing `npm create vite@latest` and selecting the `svelte-ts` option.
73+
SvelteKit が提供するすべての機能が必要ない、または望まないときは、代わりに `npm create vite@latest` と入力し、 `svelte-ts` オプションを選択することで、 Svelte 風味の Vite プロジェクトの骨組みを自動生成できます。
7474

75-
In both cases, a `svelte.config.js` with `vitePreprocess` will be added. Vite/SvelteKit will read from this config file.
75+
どちらの場合も、`vitePreprocess` を含む `svelte.config.js` が追加されます。 Vite または SvelteKit は、この設定ファイルを読み取ります。
7676

77-
### Other build tools
77+
### ほかのビルドツール <!--Other-build-tools-->
7878

79-
If you're using tools like Rollup or Webpack instead, install their respective Svelte plugins. For Rollup that's [rollup-plugin-svelte](https://github.com/sveltejs/rollup-plugin-svelte) and for Webpack that's [svelte-loader](https://github.com/sveltejs/svelte-loader). For both, you need to install `typescript` and `svelte-preprocess` and add the preprocessor to the plugin config (see the respective READMEs for more info). If you're starting a new project, you can also use the [rollup](https://github.com/sveltejs/template) or [webpack](https://github.com/sveltejs/template-webpack) template to scaffold the setup from a script.
79+
代わりに Rollup Webpack のようなツールを使っているときは、それぞれの Svelte プラグインをインストールしてください。 RollUpの場合は [rollup-plugin-svelte](https://github.com/sveltejs/rollup-plugin-svelte) 、 WebPackの場合は [svelte-loader](https://github.com/sveltejs/svelte-loader) です。両方とも、 `typescript` `svelte-preprocess` をインストールし、プリプロセッサをプラグイン設定に追加する必要があります(詳細は各READMEを参照)。新しいプロジェクトを開始するときは、 [rollup](https://github.com/sveltejs/template) または [webpack](https://github.com/sveltejs/template-webpack) テンプレートを使って、スクリプトから初期設定を自動生成することもできます。
8080

81-
> [!NOTE] If you're starting a new project, we recommend using SvelteKit or Vite instead
81+
> [!NOTE] 新しいプロジェクトを開始するときは、代わりに SvelteKit または Vite の使用をおすすめします。
8282
83-
## tsconfig.json settings
83+
## tsconfig.json の設定<!--tsconfig.json-settings-->
8484

85-
When using TypeScript, make sure your `tsconfig.json` is setup correctly.
85+
TypeScript を使うときは、 `tsconfig.json` が正しく設定されていることを確認してください。
8686

87-
- Use a [`target`](https://www.typescriptlang.org/tsconfig/#target) of at least `ES2015` so classes are not compiled to functions
88-
- Set [`verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax) to `true` so that imports are left as-is
89-
- Set [`isolatedModules`](https://www.typescriptlang.org/tsconfig/#isolatedModules) to `true` so that each file is looked at in isolation. TypeScript has a few features which require cross-file analysis and compilation, which the Svelte compiler and tooling like Vite don't do.
87+
- クラスが関数にコンパイルされないように、 [`target`](https://www.typescriptlang.org/tsconfig/#target) を少なくとも `ES2015` 以降に設定してください。
88+
- インポートがそのまま残るように、 [`verbatimModuleSyntax`](https://www.typescriptlang.org/tsconfig/#verbatimModuleSyntax) `true` に設定してください。
89+
- 各ファイルが個別に扱われるように、 [`isolatedModules`](https://www.typescriptlang.org/tsconfig/#isolatedModules) `true` に設定してください。 TypeScript にはファイル間解析とコンパイルを必要とする機能がいくつかありますが、 Svelte コンパイラや Vite などのツールではこれをおこないません。
9090

91-
## Typing `$props`
91+
## `$props` の型付け <!--Typing-$props-->
9292

93-
Type `$props` just like a regular object with certain properties.
93+
`$props` を、特定のプロパティを持つ通常のオブジェクトと同じように型付けします。
9494

9595
```svelte
9696
<script lang="ts">
@@ -118,9 +118,9 @@ Type `$props` just like a regular object with certain properties.
118118
</button>
119119
```
120120

121-
## Generic `$props`
121+
## ジェネリックな $props <!--Generic-$props-->
122122

123-
Components can declare a generic relationship between their properties. One example is a generic list component that receives a list of items and a callback property that receives an item from the list. To declare that the `items` property and the `select` callback operate on the same types, add the `generics` attribute to the `script` tag:
123+
コンポーネントのプロパティ間で、ジェネリクス(generics)を用いて型を関連づけることができます。例えば、汎用的なリストコンポーネントを考えてみましょう。このコンポーネントには、プロパティとして「アイテムのリスト」と「リストからアイテムを1つ受け取るコールバック関数」を渡すことができます。 そして、この `items` プロパティ(の配列)と `select` コールバック(の引数)が必ず同じ型であることを保証するために、 `script` タグに `generics` 属性を追加します。
124124

125125
```svelte
126126
<script lang="ts" generics="Item extends { text: string }">
@@ -139,11 +139,11 @@ Components can declare a generic relationship between their properties. One exam
139139
{/each}
140140
```
141141

142-
The content of `generics` is what you would put between the `<...>` tags of a generic function. In other words, you can use multiple generics, `extends` and fallback types.
142+
この `generics` 属性の書き方は、 TypeScript でジェネリック関数を定義するときの `<...>` (山括弧)内の構文とまったく同じです。言い換えると、複数のジェネリクス、 `extends` による型制約、デフォルトの型(フォールバック型)といった機能をそのまま利用できます。
143143

144-
## Typing wrapper components
144+
## ネイティブHTML要素をラップするコンポーネントの型付け <!--Typing-wrapper-components-->
145145

146-
In case you're writing a component that wraps a native element, you may want to expose all the attributes of the underlying element to the user. In that case, use (or extend from) one of the interfaces provided by `svelte/elements`. Here's an example for a `Button` component:
146+
コンポーネントを作るとき、ラップしたネイティブHTML要素( `button` など)の標準属性( `class``disabled` など)を、コンポーネントを使う側で自由に指定したい場合があるでしょう。そのときは `svelte/elements` が提供する型定義(インターフェース)を使用(または継承)してください。以下は `Button` コンポーネントの例です:
147147

148148
```svelte
149149
<script lang="ts">
@@ -157,7 +157,7 @@ In case you're writing a component that wraps a native element, you may want to
157157
</button>
158158
```
159159

160-
Not all elements have a dedicated type definition. For those without one, use `SvelteHTMLElements`:
160+
すべての要素に専用の型定義が用意されているわけではありません。そのような要素には、 `SvelteHTMLElements` を使います:
161161

162162
```svelte
163163
<script lang="ts">
@@ -171,23 +171,23 @@ Not all elements have a dedicated type definition. For those without one, use `S
171171
</div>
172172
```
173173

174-
## Typing `$state`
174+
## `$state` の型付け <!--Typing-$state-->
175175

176-
You can type `$state` like any other variable.
176+
`$state` はほかの変数と同じように型付けできます。
177177

178178
```ts
179179
let count: number = $state(0);
180180
```
181181

182-
If you don't give `$state` an initial value, part of its types will be `undefined`.
182+
もし `$state` に初期値を設定しない場合、その型は `undefined` とのユニオン型になります。
183183

184184
```ts
185185
// @noErrors
186-
// Error: Type 'number | undefined' is not assignable to type 'number'
186+
// エラー: 型 'number | undefined' を型 'number' に割り当てることはできません
187187
let count: number = $state();
188188
```
189189

190-
If you know that the variable _will_ be defined before you first use it, use an `as` casting. This is especially useful in the context of classes:
190+
変数の宣言時には値が決まっていなくても、後続の処理で _必ず_ 値がセットされる、とあなたが保証できるケースがあります。そのような場合は `as` キャストを使ってください。これはプロパティの初期化が `constructor` で行われるクラスで特に便利です:
191191

192192
```ts
193193
class Counter {
@@ -198,19 +198,19 @@ class Counter {
198198
}
199199
```
200200

201-
## The `Component` type
201+
## コンポーネント自体の型 <!--The-Component-type-->
202202

203-
Svelte components are of type `Component`. You can use it and its related types to express a variety of constraints.
203+
Svelte コンポーネントは `Component` 型です。 `Component` 型と関連する型を使って、さまざまな制約を表現できます。
204204

205-
Using it together with dynamic components to restrict what kinds of component can be passed to it:
205+
例えば動的コンポーネントと組み合わせ、渡せるコンポーネントの種類を制限します:
206206

207207
```svelte
208208
<script lang="ts">
209209
import type { Component } from 'svelte';
210210
211211
interface Props {
212-
// only components that have at most the "prop"
213-
// property required can be passed
212+
// "prop" プロパティが必須であるコンポーネントのみを
213+
// DynamicComponentに渡すことができます
214214
DynamicComponent: Component<{ prop: string }>;
215215
}
216216
@@ -220,9 +220,9 @@ Using it together with dynamic components to restrict what kinds of component ca
220220
<DynamicComponent prop="foo" />
221221
```
222222

223-
> [!LEGACY] In Svelte 4, components were of type `SvelteComponent`
223+
> [!LEGACY] Svelte 4 では、コンポーネントは `SvelteComponent` 型でした。
224224
225-
To extract the properties from a component, use `ComponentProps`.
225+
コンポーネントからプロパティを抽出するには、 `ComponentProps` を使います。
226226

227227
```ts
228228
import type { Component, ComponentProps } from 'svelte';
@@ -233,12 +233,12 @@ function withProps<TComponent extends Component<any>>(
233233
props: ComponentProps<TComponent>
234234
) {}
235235

236-
// Errors if the second argument is not the correct props expected
237-
// by the component in the first argument.
236+
// 第二引数が、第一引数のコンポーネントが期待するプロパティではないとき、
237+
// エラーが発生します。
238238
withProps(MyComponent, { foo: 'bar' });
239239
```
240240

241-
To declare that a variable expects the constructor or instance type of a component:
241+
コンポーネントのコンストラクタまたはインスタンス型を期待する変数を宣言するには:
242242

243243
```svelte
244244
<script lang="ts">
@@ -251,34 +251,34 @@ To declare that a variable expects the constructor or instance type of a compone
251251
<MyComponent bind:this={componentInstance} />
252252
```
253253

254-
## Enhancing built-in DOM types
254+
## 組み込みDOM型の強化 <!--Enhancing-built-in-DOM-types-->
255255

256-
Svelte provides a best effort of all the HTML DOM types that exist. Sometimes you may want to use experimental attributes or custom events coming from an action. In these cases, TypeScript will throw a type error, saying that it does not know these types. If it's a non-experimental standard attribute/event, this may very well be a missing typing from our [HTML typings](https://github.com/sveltejs/svelte/blob/main/packages/svelte/elements.d.ts). In that case, you are welcome to open an issue and/or a PR fixing it.
256+
Svelteは、存在するすべてのHTML DOMの型をできる限り提供しています。しかし、場合によっては、実験的な属性やアクション (action) 由来のカスタムイベントを使いたいことがあるかもしれません。そのようなケースでは、 TypeScript はそれらの型を認識できないため、型エラーを発生させます。もしそれが(実験的でない)標準の属性やイベントである場合、 Svelte が提供する [HTMLの型定義](https://github.com/sveltejs/svelte/blob/main/packages/svelte/elements.d.ts) にその型が定義されていない可能性があります。そのときは、ぜひ issue を立てたり、修正のためのプルリクエストを送ってください。
257257

258-
In case this is a custom or experimental attribute/event, you can enhance the typings by augmenting the `svelte/elements` module like this:
258+
もしこれがカスタムや実験的な属性・イベントであるときは、以下のように `svelte/elements` モジュールを拡張することで、型定義を強化できます:
259259

260260
```ts
261261
/// file: additional-svelte-typings.d.ts
262262
import { HTMLButtonAttributes } from 'svelte/elements';
263263

264264
declare module 'svelte/elements' {
265-
// add a new element
265+
// Svelteが認識するHTML要素のリストに、新しい要素(ここでは'custom-button')を追加します
266266
export interface SvelteHTMLElements {
267267
'custom-button': HTMLButtonAttributes;
268268
}
269269

270-
// add a new global attribute that is available on all html elements
270+
// すべてのHTML要素で利用可能な新しいグローバル属性を追加します
271271
export interface HTMLAttributes<T> {
272272
globalattribute?: string;
273273
}
274274

275-
// add a new attribute for button elements
275+
// 特定の要素(ここでは<button>要素)に新しい属性を追加します
276276
export interface HTMLButtonAttributes {
277277
veryexperimentalattribute?: string;
278278
}
279279
}
280280

281-
export {}; // ensure this is not an ambient module, else types will be overridden instead of augmented
281+
export {}; // これがアンビエントモジュールでないことを保証します。そうしないと、型が拡張(augmented)されるのではなく、上書き(overridden)されてしまいます。
282282
```
283283

284-
Then make sure that the `d.ts` file is referenced in your `tsconfig.json`. If it reads something like `"include": ["src/**/*"]` and your `d.ts` file is inside `src`, it should work. You may need to reload for the changes to take effect.
284+
次に、その `d.ts` ファイルが `tsconfig.json` で参照されていることを確認してください。もし `"include": ["src/**/*"]` のような記述があり、 `d.ts` ファイルが `src` 内にあるなら、強化した型定義は機能するはずです。変更を有効にするには、リロードが必要かもしれません。

0 commit comments

Comments
 (0)