1
- # TypeScript Support
1
+ # TypeScript のサポート
2
2
3
- > [ Vue CLI] ( https://cli.vuejs.org ) provides built-in TypeScript tooling support.
3
+ > [ Vue CLI] ( https://cli.vuejs.org ) は、ビルトインの TypeScript ツールサポートを提供します。
4
4
5
- ## Official Declaration in NPM Packages
5
+ ## NPM パッケージ内の公式型宣言
6
6
7
- A static type system can help prevent many potential runtime errors as applications grow, which is why Vue 3 is written in TypeScript. This means you don't need any additional tooling to use TypeScript with Vue - it has a first-class citizen support.
7
+ 静的型システムは、特にアプリケーションが成長するに伴い、多くの潜在的なランタイムエラーを防止するのに役立ち、これが Vue 3 が TypeScript で書かれている理由です。このことは、あなたが Vue 3 とともに TypeScript を使うために追加のツールを必要としないことを意味します - それは第一級市民 ( first-class citizen) としてサポートを受けられます。
8
8
9
- ## Recommended Configuration
9
+ ## 推奨される構成
10
10
11
11
``` js
12
12
// tsconfig.json
13
13
{
14
14
" compilerOptions" : {
15
15
" target" : " esnext" ,
16
16
" module" : " esnext" ,
17
- // this enables stricter inference for data properties on `this`
17
+ // これは `this` のデータプロパティに対してより厳密な推論を可能にします
18
18
" strict" : true ,
19
19
" jsx" : " preserve" ,
20
20
" moduleResolution" : " node"
21
21
}
22
22
}
23
23
```
24
24
25
- Note that you have to include ` strict: true ` (or at least ` noImplicitThis: true ` which is a part of ` strict ` flag) to leverage type checking of ` this ` in component methods otherwise it is always treated as ` any ` type.
25
+ コンポーネントメソッドにおける ` this ` の型チェックを活用するために ` strict: true ` (もしくは少なくとも ` strict ` フラグの一部である ` noImplicitThis: true ` ) を含める必要があることに注意してください。そうでないと、 ` this ` は常に ` any ` 型として扱われます。
26
26
27
- See [ TypeScript compiler options docs] ( https://www.typescriptlang.org/docs/handbook/compiler-options.html ) for more details.
27
+ より詳細を知るためには、 [ TypeScript compiler options docs] ( https://www.typescriptlang.org/docs/handbook/compiler-options.html ) を参照してください。
28
28
29
- ## Development Tooling
29
+ ## 開発ツール
30
30
31
- ### Project Creation
31
+ ### プロジェクトの作成
32
32
33
- [ Vue CLI] ( https://github.com/vuejs/vue-cli ) can generate new projects that use TypeScript. To get started :
33
+ [ Vue CLI] ( https://github.com/vuejs/vue-cli ) は、TypeSript を使った新しいプロジェクトを作成できます。はじめるためには :
34
34
35
35
``` bash
36
- # 1. Install Vue CLI, if it's not already installed
36
+ # 1. Vue CLI をまだインストールしていなければ、インストールします
37
37
npm install --global @vue/cli@next
38
38
39
- # 2. Create a new project, then choose the "Manually select features" option
39
+ # 2. 新しいプロジェクトを作成し、 "Manually select features" を選択してください
40
40
vue create my-project-name
41
41
42
- # If you already have a Vue CLI project without TypeScript, please add a proper Vue CLI plugin :
42
+ # もしすでに TypeScript をもたない Vue CLI で作成したプロジェクトがある場合、適切な Vue CLI プラグインを追加してください :
43
43
vue add typescript
44
44
```
45
45
46
- Make sure that ` script ` part of the component has TypeScript set as a language :
46
+ コンポーネントの ` script ` の部分に言語として TypeScript が設定されていることを確認してください :
47
47
48
48
``` html
49
49
<script lang =" ts" >
50
50
...
51
51
</script >
52
52
```
53
53
54
- ### Editor Support
54
+ ### エディタによるサポート
55
55
56
- For developing Vue applications with TypeScript, we strongly recommend using [ Visual Studio Code] ( https://code.visualstudio.com/ ) , which provides great out-of-the-box support for TypeScript. If you are using [ single-file components ] ( ./single-file-components.html ) (SFCs), get the awesome [ Vetur extension ] ( https://github.com/vuejs/vetur ) , which provides TypeScript inference inside SFCs and many other great features.
56
+ TypeScript による Vue アプリケーションの開発のために、すぐに利用できる TypeScript サポートを提供している [ Visual Studio Code] ( https://code.visualstudio.com/ ) を強く推奨します。 [ 単一ファイルコンポーネント ] ( ./single-file-components.html ) (SFCs) を使用している場合、SFC 内部での TypeScript の推論やその他の優れた機能を提供している、素晴らしい [ Vetur エクステンション ] ( https://github.com/vuejs/vetur ) を入手してください。
57
57
58
- [ WebStorm] ( https://www.jetbrains.com/webstorm/ ) also provides out-of-the-box support for both TypeScript and Vue.
58
+ [ WebStorm] ( https://www.jetbrains.com/webstorm/ ) もすぐに利用できる TypeScript と Vue のサポートを提供しています。
59
59
60
- ## Defining Vue components
60
+ ## Vue コンポーネントを定義する
61
61
62
- To let TypeScript properly infer types inside Vue component options, you need to define components with ` defineComponent ` global method :
62
+ TypeScript に Vue コンポーネントオブションのなかで適切に型を推論させるために、 ` defineComponent ` グローバルメソッドでコンポーネントを定義する必要があります :
63
63
64
64
``` ts
65
65
import { defineComponent } from ' vue'
66
66
67
67
const Component = defineComponent ({
68
- // type inference enabled
68
+ // 型推論が有効になります
69
69
})
70
70
```
71
71
72
- ## Using with Options API
72
+ ## オプション API とともに使用する
73
73
74
- TypeScript should be able to infer most of the types without defining types explicitly. For example, if you have a component with a number ` count ` property, you will have an error if you try to call a string-specific method on it :
74
+ TypeScript は明示的に型を定義することなく、ほとんどの型を推論できるようにあるべきです。例えば、数値である ` count ` プロパティを持つコンポーネントがある場合、文字列に特有のメソッドを呼び出すとエラーになります :
75
75
76
76
``` ts
77
77
const Component = defineComponent ({
@@ -81,12 +81,12 @@ const Component = defineComponent({
81
81
}
82
82
},
83
83
mounted() {
84
- const result = this .count .split (' ' ) // => Property 'split' does not exist on type 'number'
84
+ const result = this .count .split (' ' ) // => 'split' プロパティは 'number' 型に存在しません
85
85
}
86
86
})
87
87
```
88
88
89
- If you have a complex type or interface, you can cast it using [ type assertion] ( https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions ) :
89
+ 複雑な型や推論の場合、 [ タイプアサーション ( type assertion) ] ( https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions ) を使用してキャストすることができます :
90
90
91
91
``` ts
92
92
interface Book {
@@ -108,9 +108,9 @@ const Component = defineComponent({
108
108
})
109
109
```
110
110
111
- ### Annotating Return Types
111
+ ### 戻り値の型にアノテーションをつける
112
112
113
- Because of the circular nature of Vue’s declaration files, TypeScript may have difficulties inferring the types of computed. For this reason, you may need to annotate the return type computed properties.
113
+ Vue の型宣言ファイルの循環的な性質により、 TypeScript は算出プロパティの型を推論することが困難な場合があります。この理由により、算出プロパティの戻り値の型にアノテーションをつける必要があります。
114
114
115
115
``` ts
116
116
import { defineComponent } from ' vue'
@@ -122,12 +122,12 @@ const Component = defineComponent({
122
122
}
123
123
},
124
124
computed: {
125
- // needs an annotation
125
+ // アノテーションが必要です
126
126
greeting(): string {
127
127
return this .message + ' !'
128
128
}
129
129
130
- // in a computed with a setter, getter needs to be annotated
130
+ // セッターを持つ算出プロパティのでは、ゲッターにアノテーションが必要です
131
131
greetingUppercased: {
132
132
get(): string {
133
133
return this .greeting .toUpperCase ();
@@ -140,9 +140,9 @@ const Component = defineComponent({
140
140
})
141
141
```
142
142
143
- ### Annotating Props
143
+ ### Props にアノテーションをつける
144
144
145
- Vue does a runtime validation on props with a ` type ` defined. To provide these types to TypeScript, we need to cast the constructor with ` PropType ` :
145
+ Vue は ` type ` が定義されたプロパティについてランタイムバリデーションを行います。これらの型を TypeScript に提供するため、 ` PropType ` を伴うコンストラクタをキャストする必要があります :
146
146
147
147
``` ts
148
148
import { defineComponent , PropType } from ' vue'
@@ -170,11 +170,12 @@ const Component = defineComponent({
170
170
})
171
171
```
172
172
173
- If you find validator not getting type inference or member completion isn’t working, annotating the argument with the expected type may help address these problems.
173
+ バリデータの型推論やメンバの補完が機能していない場合、引数に期待される型のアノテーションをつけることで問題に対処できるかもしれません。
174
174
175
- ## Using with Composition API
175
+ ## コンポジション API とともに使用する
176
176
177
177
On ` setup() ` function, you don't need to pass a typing to ` props ` parameter as it will infer types from ` props ` component option.
178
+ ` setup() ` 関数においては、` props ` 引数に型をつける必要はありません。` setup() ` 関数は ` props ` コンポーネントオプションから型を推論するからです。
178
179
179
180
``` ts
180
181
import { defineComponent } from ' vue'
@@ -188,15 +189,16 @@ const Component = defineComponent({
188
189
},
189
190
190
191
setup(props ) {
191
- const result = props .message .split (' ' ) // correct , 'message' is typed as a string
192
- const filtered = props .message .filter (p => p .value ) // an error will be thrown : Property 'filter' does not exist on type 'string'
192
+ const result = props .message .split (' ' ) // 正しいです , 'message' は文字列 ( string) として型づけされます
193
+ const filtered = props .message .filter (p => p .value ) // エラーが起こります : Property 'filter' does not exist on type 'string'
193
194
}
194
195
})
195
196
```
196
197
197
- ### Typing ` ref ` s
198
+ ### ` ref ` を型定義する
198
199
199
200
Refs infer the type from the initial value:
201
+ Ref は初期値から肩を推論します:
200
202
201
203
``` ts
202
204
import { defineComponent , ref } from ' vue'
@@ -210,21 +212,23 @@ const Component = defineComponent({
210
212
})
211
213
```
212
214
213
- Sometimes we may need to specify complex types for a ref's inner value. We can do that simply passing a generic argument when calling ref to override the default inference :
215
+ ときどき、 ref の内部的な値の複雑な型を特定する必要があるかもしれません。デフォルトの推論をオーバーライドするために ref を呼び出すときに、単純にジェネリック引数を渡すことができます :
214
216
215
217
``` ts
216
218
const year = ref <string | number >(' 2020' ) // year's type: Ref<string | number>
219
+ const year = ref <string | number >(' 2020' ) // year の型: Ref<string | number>
217
220
218
- year .value = 2020 // ok !
221
+ year .value = 2020 // OKです !
219
222
```
220
223
221
- ::: tip Note
224
+ ::: tip ノート
222
225
If the type of the generic is unknown, it's recommended to cast ` ref ` to ` Ref<T> ` .
226
+ ジェネリックの型が不明の場合、` ref ` を ` Ref<T> ` にキャストすることを推奨します。
223
227
:::
224
228
225
- ### Typing ` reactive `
229
+ ### ` reactive ` を型定義する
226
230
227
- When typing a ` reactive ` property, we can use interfaces :
231
+ ` reactive ` プロパティを型定義する場合、推論を使用できます :
228
232
229
233
``` ts
230
234
import { defineComponent , reactive } from ' vue'
@@ -238,17 +242,17 @@ export default defineComponent({
238
242
name: ' HelloWorld' ,
239
243
setup() {
240
244
const book = reactive <Book >({ title: ' Vue 3 Guide' })
241
- // or
245
+ // もしくは
242
246
const book: Book = reactive ({ title: ' Vue 3 Guide' })
243
- // or
247
+ // もしくは
244
248
const book = reactive ({ title: ' Vue 3 Guide' }) as Book
245
249
}
246
250
})
247
251
```
248
252
249
- ### Typing ` computed `
253
+ ### 算出プロパティを型定義する
250
254
251
- Computed values will automatically infer the type from returned value
255
+ 算出プロパティの値は返り値の型から自動的に推論します:
252
256
253
257
``` ts
254
258
import { defineComponent , ref , computed } from ' vue'
@@ -258,10 +262,10 @@ export default defineComponent({
258
262
setup() {
259
263
let count = ref (0 )
260
264
261
- // read-only
265
+ // 読み取り専用
262
266
const doubleCount = computed (() => count .value * 2 )
263
267
264
- const result = doubleCount .value .split (' ' ) // => Property 'split' does not exist on type 'number'
268
+ const result = doubleCount .value .split (' ' ) // Property 'split' does not exist on type 'number'
265
269
}
266
270
})
267
271
```
0 commit comments