@@ -212,7 +212,7 @@ const Component = defineComponent({
212
212
type: Object as PropType <Book >,
213
213
// Make sure to use arrow functions
214
214
default : () => ({
215
- title: " Arrow Function Expression"
215
+ title: ' Arrow Function Expression'
216
216
}),
217
217
validator : (book : Book ) => !! book .title
218
218
},
@@ -221,7 +221,7 @@ const Component = defineComponent({
221
221
// Or provide an explicit this parameter
222
222
default(this : void ) {
223
223
return {
224
- title: " Function Expression"
224
+ title: ' Function Expression'
225
225
}
226
226
},
227
227
validator(this : void , book : Book ) {
@@ -232,6 +232,30 @@ const Component = defineComponent({
232
232
})
233
233
```
234
234
235
+ ## Annotating emits
236
+
237
+ We can annotate a payload for the emitted event. Also, all non-declared emitted events will throw a type error when called:
238
+
239
+ ``` ts
240
+ const Component = defineComponent ({
241
+ emits: {
242
+ addBook(payload : { bookName: string }) {
243
+ // perform runtime validation
244
+ return payload .bookName .length > 0
245
+ }
246
+ },
247
+ methods: {
248
+ onSubmit() {
249
+ this .$emit (' addBook' , {
250
+ bookName: 123 // Type error!
251
+ })
252
+
253
+ this .$emit (' non-declared-event' ) // Type error!
254
+ }
255
+ }
256
+ })
257
+ ```
258
+
235
259
## Using with Composition API
236
260
237
261
On ` setup() ` function, you don't need to pass a typing to ` props ` parameter as it will infer types from ` props ` component option.
0 commit comments