Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to infer component emitted event types? #56

Closed
x-255 opened this issue Mar 4, 2022 · 2 comments
Closed

How to infer component emitted event types? #56

x-255 opened this issue Mar 4, 2022 · 2 comments

Comments

@x-255
Copy link

x-255 commented Mar 4, 2022

image
image

我这里的onAdd应该是(value: number) => void,但生成的结果确是((...args: any[]) => any) | undefined,我想知道这是插件的问题还是vite打包的配置问题,应该怎么解决呢

@qmhc
Copy link
Owner

qmhc commented Mar 4, 2022

defineEmits 使用 ts 的形式只能定义返回值 emit 的类型,想要定义对外事件的类型目前只能用过用 js 的方法传入一个验证对象:

const emit = defineEmits({
  // 该函数的入参和事件完全一样,vue 内部会通过 infer 获取参数类型并赋值给 onAdd
  add: (vaule: number) => true
})

最后会得到事件类型声明如下:

{
  onAdd?: ((value: number) => any) | undefined
}

@x-255
Copy link
Author

x-255 commented Mar 4, 2022

defineEmits使用ts的类型定义只能使用过一次的返回值emit事件的当前类型,定义一个唯一的类型js验证对象:

const  emit  =  defineEmits (函数的函数和事件完全一样,vue 会通过 infer 获取类型参数输入并给定参数onAdd 
  add : number ) = > true } )
    

最后会发生的事件类型声明如下:

{ 
  onAdd ?: ( (: number )  =>  any )  |  未定义
}

有用,谢谢老哥

@qmhc qmhc changed the title emit的类型约束没有加上 How to infer component emitted event types? Mar 4, 2022
@qmhc qmhc closed this as completed Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants