How to type emit
if passed to external function?
#20108
Replies: 1 comment
-
Okay. I found a better way to do this. Instead of passing the whole // Vue component:
<script setup lang="ts">
import { markerFactory } from './my-lib/markerFactory'
export interface Emits {
(e: 'do-something', value: string): void
}
const emit = defineEmits<Emits>()
const marker = markerFactory((markerName: string) => emit('do-something', markerName))
</script>
// ./my-lib/markerFactory function:
import type { Marker } from 'someMappingLibrary`
export function markerFactory (onClickCallback: (markerName: string) => void): Marker {
const marker: Marker = new Marker({
name: 'Foo'
})
marker.addEventListener('click', () => onClickCallback(marker.name))
return marker
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey everyone,
I'm passing the
emit
that I have defined in a component to an external function. This function creates map markers on an interactive map and clicking a marker should emit an event.It works. Clicking the marker emits the
do-something
event.But as you can see, I typed the
emit
parameter asany
because the function doesn't know anything about theEmits
interface that I have defined in the component.So how would I type this right?
Thanks in advance.
EDIT: I would actually prefer to pass only the emit that should be emitted to the
markerFactory
function. How would I do that?Beta Was this translation helpful? Give feedback.
All reactions