I'm using Vue's composition API (in Vue.js 3) and build my component logic mostly within setup(). While accessing my own props via setup(props) is straight forward, I'm unable to expose the functions defined here as methods in a type-safe manner.
The following works, but I need the any-cast, since there's no method interface exposed to TypeScript.
<!-- MyComponent.vue -->
<script lang='ts'>
// ...
export default defineComponent({
setup() {
// ...
return {
publicFunction: async (): Promise<void> => { /* ... */ };
}
}
});
</script>
<!-- AppComponent.vue -->
<template>
<MyComponent ref="my"/>
</template>
<script lang='ts'>
export default defineComponent({
async setup() {
const my = ref();
async func() {
await (my.value as any).publicFunction(); // <-- gross!
}
return { my, func };
}
});
</script>
Defining my function in methods is not an option, since that way, I wouldn't be able to access it from setup.