【问题标题】:Binding element "some test text" implicitly has an "any" type绑定元素“一些测试文本”隐式具有“任何”类型
【发布时间】:2021-02-16 00:23:54
【问题描述】:

我遇到了 TS 编译器的问题,它向我抛出了这个错误:绑定元素“”隐含地具有“任何”类型。

地图函数和属性(id、title、action、backgroundColor、color)发生错误。

下面是我的代码:

export type MessageWindowComponentProps = PropsWithChildren<{
    readonly buttonsType: ButtonsType,
    readonly saveText: string,
    readonly closeText: string,
    readonly yesText: string,
    readonly noText: string,
    readonly onSaveClick?: () => void;
    readonly onCloseClick?: () => void;
    readonly onYesClick?: () => void;
    readonly onNoClick?: () => void;
}>

const classNames = bemClassNames("message-window");
const messageWindowContent = classNames("content");
const messageWindowButtons = classNames("buttons");

export const MessageWindowComponent: FunctionComponent<MessageWindowComponentProps> = ({
    children, 
    buttonsType, 
    saveText, 
    closeText, 
    yesText, 
    noText, 
    onSaveClick, 
    onCloseClick, 
    onYesClick, 
    onNoClick,
}) => {

    const typeOfButtons = {
        [ButtonsType.yesNo]: [
            {id: 1, title: {noText}, action: onYesClick, backgroundColor: ButtonBackgroundColor.whitePrimaryFixed, color: TextColor.textPrimary},
            {id: 2, title: {yesText}, action: onNoClick, backgroundColor: ButtonBackgroundColor.colorPrimary, color: TextColor.whiteBlack},
        ],
        [ButtonsType.saveClose]: [
            {id: 3, title: {closeText}, action: onCloseClick, backgroundColor: ButtonBackgroundColor.whitePrimaryFixed, color: TextColor.textPrimary},
            {id: 4, title: {saveText}, action: onSaveClick, backgroundColor: ButtonBackgroundColor.colorPrimary, color: TextColor.whiteBlack},
        ],
        [ButtonsType.close]: [
            {id: 5, title: {closeText}, action: onCloseClick, backgroundColor: ButtonBackgroundColor.colorPrimary, color: TextColor.whiteBlack},
        ],
    };

    return (
        <div className={classNames()}>
            <div className={messageWindowContent}>
                {children}
            </div>
            <div className={messageWindowButtons}>
                <Layout justifyContent={JustifyContent.flexEnd}>
                    {typeOfButtons[buttonsType].map(({id, title, action, backgroundColor, color}) => (
                        <Layout key={id} spacingSizeLeft={SpacingSize.s}>
                            <TextButton backgroundColor={backgroundColor} color={color} shadow={false} onClick={action ?? doNothing} text={title} title={title} />
                        </Layout>
                    ))}
                </Layout>
            </div>
        </div>
    );
};

感谢您的帮助。

【问题讨论】:

  • 你能在codesandbox上分享你的代码吗?这会很有帮助

标签: html reactjs typescript jsx


【解决方案1】:

编译器提示 typeOfButtons 没有指定类型。只需向这个变量添加一个类型,它应该没问题。还要检查 ButtonsType 是什么类型,以便您可以将该类型放入 typeOfButtons 类型定义中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 2019-12-13
    • 2021-05-15
    • 2020-06-19
    • 2023-01-28
    • 2021-08-26
    相关资源
    最近更新 更多