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回答
-
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)
}应该就可以啦
032021-01-23 -
qq_罗进二_0
2021-05-30
参数类型设置为可选类型 func?
const callback = ( func?:ValidateFunc ) => { if(func){ funcArr.push(func) } }
00 -
张轩
2021-01-25
谢谢同学的回答 常见问题中有这个问题
00
相似问题