English 中文(简体)
Vue 定义 Emits: 如何给函数参数提供排放值的类型
原标题:Vue defineEmits: how to give function argument the type of an emit value
If I have defined emits: const emits = defineEmits([ click-button , toggle-menu ]); Then I have a function which handles a click and takes a value which is the string that is going to be emitted: const onClick = async (emitString: string | false = false) => { if (emitString) { emits(emitString); } There is a typescript error on if (emitString) that says: Argument of type string is not assignable to parameter of type "click-button" | "toggle-menu" . So emitString needs to be typed as the union of the array values in defineEmits. How can I get such a type? I tried to change defineEmits and use a variable but it isn t possible to use local variables together with defineEmits.
最佳回答
I think you are asking for literal types: const onClick = async (emitString: "click-button" | "toggle-menu" | false = false) You can also extract it from the emits, using the Parameters utility type: const onClick = async (emitString: Parameters[0] | false = false) playground
问题回答
The error is caused by TypeScript s type check. Therefore parameter as emitString: any = false should work.




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签