Subscribe on changes!

Can't you declare multiple Defineemits at the same time when emits require different parameter types?

avatar
May 10th 2021

Version

3.1.0-beta.2

Steps to reproduce

Syntax Error: TypeError: Cannot read property 'content' of null when you declare two emits with different parameters

If only one DefineEmit exists at the same time, how do you distinguish between the type and number of arguments

import { defineProps, defineEmit, useContext } from 'vue'

const emitClose = defineEmit<(e: 'close', id: number) => void>() const emitShow = defineEmit<(e: 'show', name: string, age: number) => void>()

emitClose('close', 1) emitShow('show', '1', 18)

What is expected?

DefineEmit can exist more than one at a time

What is actually happening?

DefineEmit can have multiple errors at the same time

avatar
May 10th 2021
const emit = defineEmit<((e: 'close', id: number) => void) | ((e: 'show', name: string, age: number) => void)>()
emit('close', 1)
emit('show', '1', 18)
avatar
May 10th 2021

I tried to write it this way, but I got the same error

avatar
May 10th 2021

这样写传入的就不是一个函数类型了吧 //Syntax Error: TypeError: Cannot read property 'content' of null //type argument passed to defineEmit() must be a function type or a literal type with call signatures

avatar
May 10th 2021

const emit = defineEmit<(e: 'close', id: number) => void | ((e: 'show', name: string, age: number) => void)>()

avatar
May 10th 2021

这样写的话当我emit('show', '1', 18) 时可以触发事件 但是会有如下两个错误 vetur 会报错Expected 2 arguments, but got 3

控制台也会输出Component emitted event "show" but it is neither declared in the emits option nor as an "onShow" prop.

avatar
May 10th 2021

@yanbowe

这样:

const emit = defineEmit<{(e: 'close', id: number): void; (e: 'show', name: string, age: number): void}>()
emit('close', 1)
emit('show', '1', 2)