You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/svelte/07-misc/03-typescript.md
+53-53Lines changed: 53 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,11 +9,11 @@ title: TypeScript
9
9
- using `Component` and the other helper types
10
10
- using `svelte-check` -->
11
11
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に統合できます。
13
13
14
14
## `<script lang="ts">`
15
15
16
-
To use TypeScript inside your Svelte components, add `lang="ts"`to your `script` tags:
@@ -29,34 +29,34 @@ To use TypeScript inside your Svelte components, add `lang="ts"` to your `script
29
29
</button>
30
30
```
31
31
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:
-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
### SvelteKit または Vite の使用 <!--Using-SvelteKit-or-Vite-->
58
58
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.
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 プロジェクトの骨組みを自動生成できます。
74
74
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 は、この設定ファイルを読み取ります。
76
76
77
-
### Other buildtools
77
+
### ほかのビルドツール <!--Other-build-tools-->
78
78
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.
-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.
-各ファイルが個別に扱われるように、[`isolatedModules`](https://www.typescriptlang.org/tsconfig/#isolatedModules)を`true`に設定してください。 TypeScript にはファイル間解析とコンパイルを必要とする機能がいくつかありますが、 Svelte コンパイラや Vite などのツールではこれをおこないません。
90
90
91
-
## Typing `$props`
91
+
## `$props` の型付け <!--Typing-$props-->
92
92
93
-
Type `$props`just like a regular object with certain properties.
93
+
`$props`を、特定のプロパティを持つ通常のオブジェクトと同じように型付けします。
94
94
95
95
```svelte
96
96
<script lang="ts">
@@ -118,9 +118,9 @@ Type `$props` just like a regular object with certain properties.
118
118
</button>
119
119
```
120
120
121
-
## Generic `$props`
121
+
## ジェネリックな $props<!--Generic-$props-->
122
122
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:
@@ -139,11 +139,11 @@ Components can declare a generic relationship between their properties. One exam
139
139
{/each}
140
140
```
141
141
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.
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:
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.
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.
0 commit comments