mitt又报错了

来源:5-12 ValidateForm 编码第四部分 - 大功告成

随心无憾

2021-01-23

图片描述

ERROR in src/components/ValidateForm.vue:29:5
TS2769: No overload matches this call.
Overload 1 of 2, ‘(type: “", handler: WildcardHandler): void’, gave the following error.
Argument of type ‘“form-item-cteated”’ is not assignable to parameter of type '"
”’.
Overload 2 of 2, ‘(type: string | symbol, handler: Handler): void’, gave the following error.
Argument of type ‘(func: ValidateFunc) => void’ is not assignable to parameter of type ‘Handler’.
Types of parameters ‘func’ and ‘event’ are incompatible.
Type ‘ValidateFunc | undefined’ is not assignable to type ‘ValidateFunc’.
Type ‘undefined’ is not assignable to type ‘ValidateFunc’.
27 | }
28 | };

29 | emitter.on(“form-item-cteated”, callback);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30 | onUnmounted(() => {
31 | emitter.off(“form-item-cteated”, callback);
32 | funcArr = [];

ERROR in src/components/ValidateForm.vue:31:7
TS2769: No overload matches this call.
Overload 1 of 2, ‘(type: “", handler: WildcardHandler): void’, gave the following error.
Argument of type ‘“form-item-cteated”’ is not assignable to parameter of type '"
”’.
Overload 2 of 2, ‘(type: string | symbol, handler: Handler): void’, gave the following error.
Argument of type ‘(func: ValidateFunc) => void’ is not assignable to parameter of type ‘Handler’.
29 | emitter.on(“form-item-cteated”, callback);
30 | onUnmounted(() => {

31 | emitter.off(“form-item-cteated”, callback);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32 | funcArr = [];
33 | });
34 | return {

写回答

3回答

qq_顺其自然_67

2021-01-23

mitt 的定义文件有所升级,打开 mitt 的定义文件可以看到,on 的定义

on<T = any>(type: EventType, handler: Handler<T>): void;

type Handler<T = any> = (event?: T) => void;

发现 Handler 这个类型中 event 参数是可选的。

我们不能把(text: string)=> void(你的类型) 赋值给 (event?: T) => void 类型

所以只要修改一下 ,改成

   const callback = (text?: string) => {
     console.log(text)
   }

应该就可以啦


0
3
随心无憾
回复
qq_顺其自然_67
在func ?:ValidateFunc;就解决了
2021-01-23
共3条回复

qq_罗进二_0

2021-05-30

参数类型设置为可选类型 func?

const callback = ( func?:ValidateFunc ) => {
  if(func){
    funcArr.push(func)
  }
}


0
0

张轩

2021-01-25

谢谢同学的回答 常见问题中有这个问题 

0
0

Vue3 + TS 仿知乎专栏企业级项目

带你完成前后端分离复杂项目,率先掌握 vue3 造轮子技能

3142 学习 · 2313 问题

查看课程