Problem description
The error message is as follows:
TS2769: No overload matches this call.
Overload 1 of 2, '(type: "*", handler: WildcardHandler<Record<EventType, unknown>>): void', gave the following error.
Argument of type '"form-item-created"' is not assignable to parameter of type '"*"'.
Overload 2 of 2, '(type: "form-item-created", handler?: Handler<unknown> | undefined): void', gave the following error.
Argument of type '(func: ValidateFunc) => void' is not assignable to parameter of type 'Handler<unknown>'.
47 | onUnmounted(() => {
48 |
> 49 | emitter.off('form-item-created', callback)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50 | funcArr = []
51 | })
52 |
reason:
Mitt’s definition file has been upgraded
Solution:
Code before modification
import mitt from 'mitt'
type ValidateFunc = () => boolean
export const emitter = mitt()
emitter.emit('formItemCreated', validateInput)
const callback = (func: ValidateFunc) => {
funcArr.push(func)
}
emitter.on('formItemCreated', callback)
onUnmounted(() => {
emitter.off('formItemCreated', callback)
funcArr = []
})
Modified code
import mitt from 'mitt'
type ValidateFunc = () => boolean
export const emitter = mitt<{
formItemCreated: ValidateFunc
}>()
...
Or use the following code
import mitt from 'mitt'
type ValidateFunc = () => boolean
type Emits<EventType extends string | symbol, T> = {
on(type: EventType, handler: (arg: T) => void): void
off(type: EventType, handler: (arg: T) => void): void
emit(type: EventType, arg: T): void
}
// type Emitter = Emits<'a', number> & Emits<'b', string>;
type Emitter = Emits<'form-item-created', ValidateFunc>
export const emitter: Emitter = mitt<Emitter>()
...