请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

mitt又报错了

图片描述

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回答

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 回复 有任何疑惑可以回复我~
qq_罗进二_0 2021-05-30 18:37:28

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

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


0 回复 有任何疑惑可以回复我~
张轩 2021-01-25 17:21:50

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

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信