Function: emitsFactory()
ts
function emitsFactory<T>(validators: T): EmitFunctions<T>;Factory function that converts emit validator functions to Vue-compatible emit functions.
This utility solves the TypeScript integration challenge with Vue's emit system:
- Vue expects emit functions to return
void - Developers want validator functions that return
booleanfor validation logic - This factory preserves runtime validation while providing correct TypeScript types
Type Parameters
| Type Parameter | Description |
|---|---|
T extends EmitValidators | The type of the validators object |
Parameters
| Parameter | Type | Description |
|---|---|---|
validators | T | Object containing validator functions that return boolean |
Returns
EmitFunctions<T>
The same object cast to have emit functions that return void
Example
typescript
// Define validators with actual validation logic
const validators = {
"update:modelValue": (value: string[]) => Array.isArray(value),
error: (error: unknown) => true, // Always allow error emissions
save: (data: SaveData) => data && typeof data.id === "string",
};
// Use in Vue component
export default defineComponent({
emits: emitsFactory(validators),
setup(props, { emit }) {
// emit functions are properly typed as (...args) => void
emit("update:modelValue", ["item1", "item2"]);
emit("error", new Error("Something went wrong"));
},
});