@@ -131,7 +131,7 @@ const Component = defineComponent({
131
131
})
132
132
```
133
133
134
- 複雑な型や推論の場合、[ タイプアサーション (type assertion)] ( https://www.typescriptlang.org/docs/handbook/basic -types.html#type-assertions ) を使用してキャストすることができます:
134
+ 複雑な型や推論の場合、[ タイプアサーション (type assertion)] ( https://www.typescriptlang.org/docs/handbook/2/everyday -types.html#type-assertions ) を使用してキャストすることができます:
135
135
136
136
``` ts
137
137
interface Book {
@@ -221,10 +221,10 @@ const Component = defineComponent({
221
221
// セッターを持つ算出プロパティのでは、ゲッターにアノテーションが必要です
222
222
greetingUppercased: {
223
223
get(): string {
224
- return this .greeting .toUpperCase ();
224
+ return this .greeting .toUpperCase ()
225
225
},
226
226
set(newValue : string ) {
227
- this .message = newValue .toUpperCase ();
227
+ this .message = newValue .toUpperCase ()
228
228
}
229
229
}
230
230
}
@@ -260,7 +260,7 @@ const Component = defineComponent({
260
260
```
261
261
262
262
::: warning
263
- TypeScript には、関数式の型推論に [ 設計上の制限] ( https://github.com/microsoft/TypeScript/issues/38845 ) があるため、 ` validators ` と、オブジェクトや配列の ` default ` 値に注意する必要があります:
263
+ TypeScript には、関数式の型推論に [ 設計上の制限] ( https://github.com/microsoft/TypeScript/issues/38845 ) があるため、 ` validator ` と、オブジェクトや配列の ` default ` 値に注意する必要があります:
264
264
:::
265
265
266
266
``` ts
@@ -297,7 +297,7 @@ const Component = defineComponent({
297
297
})
298
298
```
299
299
300
- ### emits にアノテーションをつける
300
+ ### Emit にアノテーションをつける
301
301
302
302
発行されたイベントのペイロードにアノテーションをつけることができます。また、すべての宣言されていない発行されたイベントは、呼び出されたときに型エラーが発生します:
303
303
@@ -372,6 +372,77 @@ year.value = 2020 // OKです!
372
372
ジェネリックの型が不明の場合、` ref ` を ` Ref<T> ` にキャストすることを推奨します。
373
373
:::
374
374
375
+ ### テンプレート参照を型定義する
376
+
377
+ ときどき、子コンポーネントのパブリックメソッドを呼び出すために、テンプレート参照にアノテーションをつける必要があるかもしれません。例えば、 ` MyModal ` という子コンポーネントにモーダルを開くメソッドがあるとします:
378
+
379
+ ``` ts
380
+ import { defineComponent , ref } from ' vue'
381
+
382
+ const MyModal = defineComponent ({
383
+ setup() {
384
+ const isContentShown = ref (false )
385
+ const open = () => (isContentShown .value = true )
386
+
387
+ return {
388
+ isContentShown ,
389
+ open
390
+ }
391
+ }
392
+ })
393
+ ```
394
+
395
+ この親コンポーネントからテンプレート参照を介して、このメソッドを呼び出したいです:
396
+
397
+ ``` ts
398
+ import { defineComponent , ref } from ' vue'
399
+
400
+ const MyModal = defineComponent ({
401
+ setup() {
402
+ const isContentShown = ref (false )
403
+ const open = () => (isContentShown .value = true )
404
+
405
+ return {
406
+ isContentShown ,
407
+ open
408
+ }
409
+ }
410
+ })
411
+
412
+ const app = defineComponent ({
413
+ components: {
414
+ MyModal
415
+ },
416
+ template: `
417
+ <button @click="openModal">Open from parent</button>
418
+ <my-modal ref="modal" />
419
+ ` ,
420
+ setup() {
421
+ const modal = ref ()
422
+ const openModal = () => {
423
+ modal .value .open ()
424
+ }
425
+
426
+ return { modal , openModal }
427
+ }
428
+ })
429
+ ```
430
+
431
+ この方法でも動作しますが、 ` MyModal ` とその利用可能なメソッドについての型情報がありません。これを解決するためには、 ref を作成するときに ` InstanceType ` を使う必要があります:
432
+
433
+ ``` ts
434
+ setup () {
435
+ const modal = ref <InstanceType <typeof MyModal >>()
436
+ const openModal = () => {
437
+ modal .value ?.open ()
438
+ }
439
+
440
+ return { modal , openModal }
441
+ }
442
+ ```
443
+
444
+ [ オプショナルチェイニング] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining ) や他の方法を使って、 ` modal.value ` が未定義でないことの確認が必要であることに注意してください。
445
+
375
446
### ` reactive ` を型定義する
376
447
377
448
` reactive ` プロパティを型定義する場合、推論を使用できます:
0 commit comments