reactjs - 如何在样式组件中使用 Typescript 自定义 Prop

标签 reactjs typescript styled-components

我有一个 Button 组件,一个扩展基本 Button 的 DayButton,最后是一个实例化 DayButtons 的组件。

//button.tsx

export const StyledButton = styled.button`
  ...
`;

export interface ButtonProps {
  children?: JSX.Element;
  onClick?: () => void;
}

function Button({ children, onClick }: ButtonProps): JSX.Element {
  return <StyledButton onClick={onClick}>{children}</StyledButton>;
}
//day-button.tsx

const StyledDayButton = styled(StyledButton)`
    ...
`;

export interface DayButtonProps extends ButtonProps {
    date: Date;
    notificationCount: number;
}

function DayButton({ onClick, date, notificationCount }: DayButtonProps): JSX.Element {
    return (
        <StyledDayButton onClick={onClick}>
//            <StyledDayText>{date.toLocaleString('default', { day: 'numeric' })}</StyledDayText>
//            <StyledNotificationContainer>
//                <NotificationIndicator />
//                <StyledNotificationCounter>{notificationCount}</StyledNotificationCounter>
//            </StyledNotificationContainer>
        </StyledDayButton>
    );
}
//day-picker.tsx

const StyledDayPicker = styled.div`
    ...
`;

function DayPicker(): JSX.Element {
    return (
        <StyledDayPicker>
            <DayButton date={new Date()} notificationCount={268} />
        </StyledDayPicker>
    );
}

我希望能够像这样在日期按钮样式部分使用“日期” Prop :

//day-button.tsx


const StyledDayButton = styled(StyledButton)<DayButtonProps>`
    ...
    background-color: ${(props: DayButtonProps)=> someBoolFunction(props.date) && 'transparent'}
`;

不幸的是,我收到此错误消息:

TypeScript error in /home/maplesyrup/git/seam/seam-frontend/src/components/shared/day-button.tsx(40,10):
No overload matches this call.
  Overload 1 of 2, '(props: Pick<Pick<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; } & DayButtonProps, "form" | ... 267 more ... | "notificationCount"> & Partial<...>, "form" | ... 267 more ... | "notificationCount"> & { ...; } & { ...; }): ReactElement<...>', gave the following error.
    Type '{ children: Element[]; onClick: (() => void) | undefined; }' is missing the following properties from type 'Pick<Pick<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; } & DayButtonProps, "form" | ... 267 more ... | "notificationCount"> & Partial<...>, "form" | ... 267 more ... | "notificationCount">': date, notificationCount
  Overload 2 of 2, '(props: StyledComponentPropsWithAs<"button", DefaultTheme, DayButtonProps, never>): ReactElement<StyledComponentPropsWithAs<"button", DefaultTheme, DayButtonProps, never>, string | ... 1 more ... | (new (props: any) => Component<...>)>', gave the following error.
    Type '{ children: Element[]; onClick: (() => void) | undefined; }' is missing the following properties from type 'Pick<Pick<Pick<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "form" | ... 264 more ... | "value"> & { ...; } & DayButtonProps, "form" | ... 267 more ... | "notificationCount"> & Partial<...>, "form" | ... 267 more ... | "notificationCount">': date, notificationCount  TS2769

    38 | function DayButton({ onClick, date, notificationCount }: DayButtonProps): JSX.Element {
    39 |     return (
  > 40 |         <StyledDayButton onClick={onClick}>
       |          ^
    41 |             <StyledDayText>{date.toLocaleString('default', { day: 'numeric' })}</StyledDayText>
    42 |             <StyledNotificationContainer>
    43 |                 <NotificationIndicator />

我在语法方面做错了什么吗?我探索了 thread关于此正在寻找解决方案,但未能找到有效的解决方案。

最佳答案

interface Props {
  schema: any;
  onSubmit: Function;
  [key: string]: any; // with any number of properties with any
}

关于reactjs - 如何在样式组件中使用 Typescript 自定义 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63224865/

相关文章:

javascript - 故事书自定义字体未显示

javascript - 无法使用 webpack 将 Base64 图像导入到 React 组件中

javascript - 使用: and => for the return type with a TypeScript function?有什么区别

reactjs - 如何修复 'Static HTML elements with event handlers require a role.'?

CSS 溢出无法按预期工作并且无法滚动到 div 的底部

javascript - 解构对象内的所有值

css - 在 React Griddle 中选择一行,并更改 tr 背景颜色

5 分钟内的 Typescript 卡在 Classes 部分

angular - RxJS 6+ 类型 'Observable' 必须有一个返回迭代器的 'Symbol.iterator' 方法

javascript - 制作带样式的组件 DRYer