Skip to content

Commit e10ad66

Browse files
authored
Merge pull request #600 from svelte-jp/cherry-pick-patch
cherry-pick to svelte-4 branch
2 parents c960892 + 91bd2e8 commit e10ad66

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

documentation/docs/05-misc/03-typescript.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
title: TypeScript
33
---
44

5-
You can use TypeScript within Svelte components. IDE extensions like the [Svelte VSCode 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.
5+
Svelte コンポーネント内では TypeScript が使用できます。[Svelte VSCode extension](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode) などの IDE の拡張機能を使用すると、エディター上でエラーをすぐに見つけやすくなります。また、[`svelte-check`](https://www.npmjs.com/package/svelte-check) は同じことをコマンドライン上で実行できるため、CI と統合できます。
66

7-
## Setup
7+
## セットアップ <!--setup-->
88

9-
To use TypeScript within Svelte components, you need to add a preprocessor that will turn TypeScript into JavaScript.
9+
Svelte コンポーネント内で TypeScript を使用するには、TypeScript JavaScript に変換するプリプロセッサーを追加する必要があります。
1010

11-
### Using SvelteKit or Vite
11+
### SvelteKit または Vite を使用する <!--using-sveltekit-or-vite-->
1212

13-
The easiest way to get started is scaffolding a new SvelteKit project by typing `npm create svelte@latest`, following the prompts and choosing the TypeScript option.
13+
TypeScript を使い始めるのに最も簡単な方法は、`npm create svelte@latest` とタイプして、新しい SvelteKit プロジェクトを scaffold し、プロンプトに従って TypeScript オプションを選択することです。
1414

1515
```ts
1616
/// file: svelte.config.js
@@ -24,7 +24,7 @@ const config = {
2424
export default config;
2525
```
2626

27-
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.
27+
SvelteKit が提供するすべての機能が必要ではない場合は、`npm create vite@latest` とタイプして `svelte-ts` オプションを選ぶことで、Svelte 向けの Vite プロジェクトを scaffold できます。
2828

2929
```ts
3030
/// file: svelte.config.js
@@ -54,17 +54,17 @@ const config = {
5454
export default config;
5555
```
5656

57-
In both cases, a `svelte.config.js` with `vitePreprocess` will be added. Vite/SvelteKit will read from this config file.
57+
いずれの場合でも、`vitePreprocess` を使用した `svelte.config.js` が追加されます。Vite/SvelteKit はこの設定ファイルを読み込みます。
5858

59-
### Other build tools
59+
### その他のビルドツール <!--other-build-tools-->
6060

61-
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.
61+
代わりに 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) のテンプレートを使ってスクリプトからセットアップを scaffold することもできます。
6262

63-
> If you're starting a new project, we recommend using SvelteKit or Vite instead
63+
> 新しいプロジェクトを開始する場合は、代わりに SvelteKit または Vite を使うことをおすすめします。
6464
6565
## `<script lang="ts">`
6666

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

6969
```svelte
7070
<script lang="ts">
@@ -78,7 +78,7 @@ To use TypeScript inside your Svelte components, add `lang="ts"` to your `script
7878

7979
### Props
8080

81-
Props can be typed directly on the `export let` statement:
81+
Props は、`export let` 文に直接型付けできます。
8282

8383
```svelte
8484
<script lang="ts">
@@ -88,7 +88,7 @@ Props can be typed directly on the `export let` statement:
8888

8989
### Slots
9090

91-
Slot and slot prop types are inferred from the types of the slot props passed to them:
91+
Slot slot prop の型は、渡された slot props の型から推論されます。
9292

9393
```svelte
9494
<script lang="ts">
@@ -99,23 +99,23 @@ Slot and slot prop types are inferred from the types of the slot props passed to
9999
100100
<!-- Later -->
101101
<Comp let:name>
102-
<!-- ^ Inferred as string -->
102+
<!-- ^ string として推論される -->
103103
{name}
104104
</Comp>
105105
```
106106

107107
### Events
108108

109-
Events can be typed with `createEventDispatcher`:
109+
Event は `createEventDispatcher` を使って型付けできます。
110110

111111
```svelte
112112
<script lang="ts">
113113
import { createEventDispatcher } from 'svelte';
114114
115115
const dispatch = createEventDispatcher<{
116-
event: null; // does not accept a payload
117-
click: string; // has a required string payload
118-
type: string | null; // has an optional string payload
116+
event: null; // payload を受け付けない
117+
click: string; // 必須の string payload を持つ
118+
type: string | null; // オプションの string payload を持つ
119119
}>();
120120
121121
function handleClick() {
@@ -132,32 +132,32 @@ Events can be typed with `createEventDispatcher`:
132132
<button on:click={handleClick} on:keydown={handleType}>Click</button>
133133
```
134134

135-
## Enhancing built-in DOM types
135+
## ビルトインのDOM型を拡張する <!--enhancing-built-in-dom-types-->
136136

137-
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/master/packages/svelte/elements.d.ts). In that case, you are welcome to open an issue and/or a PR fixing it.
137+
Svelte はベストエフォートで存在する全ての HTML DOM の型を提供します。ときには実験的な属性やアクションから来るカスタムイベントを使いたい場合があるかもしれません。そのような場合には、TypeScript が型エラーを発生し、未知の型であると報告します。もし実験的ではない標準の属性やイベントであるなら、Svelte の [HTML ](https://github.com/sveltejs/svelte/blob/master/packages/svelte/elements.d.ts) から来た型付けの誤りによる可能性があります。その場合、issue や修正の PR を歓迎します。
138138

139-
In case this is a custom or experimental attribute/event, you can enhance the typings like this:
139+
これがカスタムまたは実験的な属性またはイベントの場合、以下のように型を拡張できます。
140140

141141
```ts
142142
/// file: additional-svelte-typings.d.ts
143143
declare namespace svelteHTML {
144-
// enhance elements
144+
// 要素の拡張
145145
interface IntrinsicElements {
146146
'my-custom-element': { someattribute: string; 'on:event': (e: CustomEvent<any>) => void };
147147
}
148-
// enhance attributes
148+
// 属性の拡張
149149
interface HTMLAttributes<T> {
150-
// If you want to use on:beforeinstallprompt
150+
// on:beforeinstallprompt を使用したい場合
151151
'on:beforeinstallprompt'?: (event: any) => any;
152-
// If you want to use myCustomAttribute={..} (note: all lowercase)
153-
mycustomattribute?: any; // You can replace any with something more specific if you like
152+
// myCustomAttribute={..} (注意: すべて小文字) を使用したい場合
153+
mycustomattribute?: any; // 望むなら any をより特定の型に置き換えられます
154154
}
155155
}
156156
```
157157

158-
Then make sure that `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.
158+
そして、`d.ts` ファイルが `tsconfig.json` で参照されるようにします。`"include": ["src/**/*"]` のような設定があり、`d.ts` ファイルが `src` 内にあれば、上手く動作するはずです。変更を反映するためには再読み込みが必要なことがあります。
159159

160-
Since Svelte version 4.2 / `svelte-check` version 3.5 / VS Code extension version 107.10.0 you can also declare the typings by augmenting the `svelte/elements` module like this:
160+
Svelte バージョン 4.2 / `svelte-check` バージョン 3.5 / VS Code 拡張機能 107.10.0 以降では、以下のように `svelte/elements` モジュールを拡張することでも型を宣言できるようになりました。
161161

162162
```ts
163163
/// file: additional-svelte-typings.d.ts
@@ -168,52 +168,52 @@ declare module 'svelte/elements' {
168168
'custom-button': HTMLButtonAttributes;
169169
}
170170

171-
// allows for more granular control over what element to add the typings to
171+
// 型を追加する要素へのより細かい制御を可能にする
172172
export interface HTMLButtonAttributes {
173173
veryexperimentalattribute?: string;
174174
}
175175
}
176176

177-
export {}; // ensure this is not an ambient module, else types will be overridden instead of augmented
177+
export {}; // これが ambient module ではなく、他の型が拡張される代わりに上書きされることを保証する
178178
```
179179

180-
## Experimental advanced typings
180+
## 実験的な高度な型付け <!--experimental-advanced-typings-->
181181

182-
A few features are missing from taking full advantage of TypeScript in more advanced use cases like typing that a component implements a certain interface, explicitly typing slots, or using generics. These things are possible using experimental advanced type capabilities. See [this RFC](https://github.com/dummdidumm/rfcs/blob/ts-typedefs-within-svelte-components/text/ts-typing-props-slots-events.md) for more information on how to make use of them.
182+
特定のインターフェイスを実装するコンポーネントの型付け、明示的に型付けされた slot、ジェネリクスの使用など、より高度なユースケースで TypeScript を最大限に活用するには、いくつかの機能が欠けています。これらの機能は、実験的な高度な型機能を利用することで実現可能です。使用方法に関するより詳しい情報は、[この RFC](https://github.com/dummdidumm/rfcs/blob/ts-typedefs-within-svelte-components/text/ts-typing-props-slots-events.md) を参照してください。
183183

184-
> The API is experimental and may change at any point
184+
> API は実験的であるため、いつでも変更される可能性があります
185185
186-
## Limitations
186+
## 制限事項 <!--limitations-->
187187

188-
### No TS in markup
188+
### マークアップ内ではTSは使えない <!--no-ts-in-markup-->
189189

190-
You cannot use TypeScript in your template's markup. For example, the following does not work:
190+
TypeScript はテンプレートのマークアップ内では使えません。たとえば、次のコードは機能しません。
191191

192192
```svelte
193193
<script lang="ts">
194194
let count = 10;
195195
</script>
196196
197-
<h1>Count as string: {count as string}!</h1> <!-- ❌ Does not work -->
197+
<h1>Count as string: {count as string}!</h1> <!-- ❌ 動かない -->
198198
{#if count > 4}
199-
{@const countString: string = count} <!-- ❌ Does not work -->
199+
{@const countString: string = count} <!-- ❌ 動かない -->
200200
{countString}
201201
{/if}
202202
```
203203

204-
### Reactive Declarations
204+
### リアクティブな宣言 <!--reactive-declarations-->
205205

206-
You cannot type your reactive declarations with TypeScript in the way you type a variable. For example, the following does not work:
206+
TypeScript を使用したリアクティブな宣言に対しては、変数と同じように型付けすることはできません。たとえば、次のコードは動作しません。
207207

208208
```svelte
209209
<script lang="ts">
210210
let count = 0;
211211
212-
$: doubled: number = count * 2; // ❌ Does not work
212+
$: doubled: number = count * 2; // ❌ 動かない
213213
</script>
214214
```
215215

216-
You cannot add a `: TYPE` because it's invalid syntax in this position. Instead, you can move the definition to a `let` statement just above:
216+
この位置では無効な構文となるため、`: TYPE` を追加することはできません。その代わり、型の定義を直前の `let` 文に移動できます。
217217

218218
```svelte
219219
<script lang="ts">

0 commit comments

Comments
 (0)