Skip to content

Commit 6a0a845

Browse files
authored
Example for new defineEmit with types syntax.md (vuejs#2382)
1 parent 870ac43 commit 6a0a845

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/guide/typescript/composition-api.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,21 @@ const emit = defineEmits<{
148148
(e: 'change', id: number): void
149149
(e: 'update', value: string): void
150150
}>()
151+
152+
// 3.3+: alternative, more succinct syntax
153+
const emit = defineEmits<{
154+
change: [id: number]
155+
update: [value: string]
156+
}>()
151157
</script>
152158
```
153159

154-
The type argument should be a type literal with [Call Signatures](https://www.typescriptlang.org/docs/handbook/2/functions.html#call-signatures). The type literal will be used as the type of the returned `emit` function. As we can see, the type declaration gives us much finer-grained control over the type constraints of emitted events.
160+
The type argument can be one of the following:
161+
162+
1. A callable function type, but written as a type literal with [Call Signatures](https://www.typescriptlang.org/docs/handbook/2/functions.html#call-signatures). It will be used as the type of the returned `emit` function.
163+
2. A type literal where the keys are the event names, and values are array / tuple types representing the additional accepted parameters for the event. The example above is using named tuples so each argument can have an explicit name.
164+
165+
As we can see, the type declaration gives us much finer-grained control over the type constraints of emitted events.
155166

156167
When not using `<script setup>`, `defineComponent()` is able to infer the allowed events for the `emit` function exposed on the setup context:
157168

0 commit comments

Comments
 (0)