【问题标题】:React TypeScript with antd用 antd 反应 TypeScript
【发布时间】:2021-02-17 09:30:27
【问题描述】:

这是我的代码

import { notification } from "antd";


const openNotificationWithIcon = (type: string, message: string, err: string) => {
    if(type == 'success' || type == 'warning')
        notification[type] = ({ message: message, description: err}) *//error here*
    return;
};

export default openNotificationWithIcon;

但消息有错误

Type '{ message: string; description: string; }' is not assignable to type '((args: ArgsProps) => void) & ((args: ArgsProps) => void)'.
  Object literal may only specify known properties, and 'message' does not exist in type '((args: ArgsProps) => void) & ((args: ArgsProps) => void)'

我在 antd 中看到该消息需要 ReactNode 类型,我已修复但没有发生

【问题讨论】:

    标签: reactjs types antd


    【解决方案1】:

    好像是notificitaion['success' | '警告'] 是一个函数。这意味着您需要调用它而不是分配。

    所以正确的使用方法是

     import { notification } from "antd";
    
    
     const openNotificationWithIcon = (type: string, message: string, err: string) => {
        if(type == 'success' || type == 'warning')
            notification[type]({ message: message, description: err});
        return;
    };
    
    export default openNotificationWithIcon;
    

    【讨论】:

    • 谢谢,但是为什么当我添加'='时会发生这个错误,你能解释一下吗
    • 因为你试图将一个对象分配给一个函数类型你不能做 notification[type] = ({ message: message, description: err}) 而是尝试 notification[type] = ({ message : message, description: err}) => { //anything that you want to do here } 但正确的方法仍然是像我在答案中显示的那样调用,因为它是一个要调用的函数。 @PhươngTrần
    • @YunusEmre,你能看看这里吗stackoverflow.com/questions/66963268/…
    猜你喜欢
    • 1970-01-01
    • 2021-09-23
    • 2020-07-27
    • 2020-11-14
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多