Vue Tip: Typing Component Events
Michael Hoffmann
@mokkapps
To declare emits
with full type inference support, we can use the defineEmits
API, which is automatically available inside <script setup>
:
<script setup>
const emit = defineEmits(['change', 'delete'])
</script>
defineEmits
is a compiler macro only usable inside <script setup>
. It does not need to be imported and is compiled away when <script setup>
is processed. defineEmits
provide proper type inference based on the options passed.
Using TypeScript, it is also possible to declare props and emits using pure type annotations:
<script setup>
const emit = defineEmits<{
(e: 'change', id: number): void
(e: 'delete', value: string): void
}>()
</script>
defineEmits
can only use either runtime declaration OR type declaration. Using both at the same time will result in a compile error.
- A type literal
- A reference to an interface or a type literal in the same file ::
If you liked this Vue tip, follow me on BlueSky to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter :