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: documentation/docs/05-misc/03-typescript.md
+42-42Lines changed: 42 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,15 @@
2
2
title: TypeScript
3
3
---
4
4
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 と統合できます。
6
6
7
-
## Setup
7
+
## セットアップ
8
8
9
-
To use TypeScript within Svelte components, you need to add a preprocessor that will turn TypeScript into JavaScript.
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.
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 できます。
28
28
29
29
```ts
30
30
/// file: svelte.config.js
@@ -37,17 +37,17 @@ const config = {
37
37
exportdefaultconfig;
38
38
```
39
39
40
-
In both cases, a `svelte.config.js`with `vitePreprocess` will be added. Vite/SvelteKit will read from this config file.
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.
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.
120
+
Svelte はベストエフォートで存在する全ての HTML DOM の型を提供します。ときには実験的な属性やアクションから来るカスタムイベントを使いたい場合があるかもしれません。そのような場合には、TypeScript が型エラーを発生し、未知の型であると報告します。もし実験的ではない標準の属性やイベントであるなら、Svelte の [HTML 型](https://github.com/sveltejs/svelte/blob/master/packages/svelte/elements.d.ts) から来た型付けの誤りによる可能性があります。その場合、issue や修正の PR を歓迎します。
121
121
122
-
In case this is a custom or experimental attribute/event, you can enhance the typings like this:
//If you want to use myCustomAttribute={..} (note: all lowercase)
136
-
mycustomattribute?:any; //You can replace any with something more specific if you like
135
+
// myCustomAttribute={..} (注意: すべて小文字) を使用したい場合
136
+
mycustomattribute?:any; //望むなら any をより特定の型に置き換えられます
137
137
}
138
138
}
139
139
```
140
140
141
-
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.
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:
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.
0 commit comments