繁体   English   中英

当尝试使用 typescript 定义功能组件以与样式组件反应时,会出现“没有重载匹配此调用”的错误。

[英]when try to define a functional component in react with styled-components using typescript get error of “No overload matches this call.”

沙箱https://codesandbox.io/s/typescript-type-checking-question-0b42t

代码

type BadgeTypes = {
  success: string;
  secondary: string;
  alert: string;
  text: string;
};

type Theme = {
  fonts?: object;
  borderRadius: string;
  primary?: object;
  accent?: object;
  action?: object;
  stories?: object;
  checkbox?: object;
  badge: BadgeTypes;
  button?: object;
  page?: object;
  states?: object;
  modal?: object;
};
interface Props {
  theme: Theme;
  large: boolean;
  type: keyof BadgeTypes;
}
const StyledBadge = styled.span<Props>`
  display: inline-block;
  border-radius: ${(props: Props): string => props.theme.borderRadius};
  text-align: center;
  color: ${(props: Props): string => props.theme.badge.text};
  font-size: ${(props: Props): string => (props.large ? `1.4rem` : `1rem`)};
  padding: ${(props: Props): string =>
    props.large ? `0 .8rem` : `0.2rem 0.6rem 0.3rem 0.6rem`};
  background: ${(props: Props): string => props.theme.badge[props.type]};
`;
interface BadgeProps {
  children: React.ReactChildren;
  // any other props that come into the component
}

const Badge = ({
  children,
  ...props
//problem likely the line below
}: BadgeProps): React.FunctionComponentElement<BadgeProps> => (
  <StyledBadge {...props}>{children}</StyledBadge>
);

错误是

No overload matches this call.
  Overload 1 of 2, '(props: Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, "slot" | "style" | ... 253 more ... | "is"> & { ...; } & Props, "slot" | ... 258 more ... | "large"> & Partial<...>, "slot" | ... 257 more ... | "large"> & { ...; } & { ...; }): ReactElement<...>', gave the following error.
    Type '{ children: ReactChildren; }' is missing the following properties from type 'Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, "slot" | "style" | ... 253 more ... | "is"> & { ...; } & Props, "slot" | ... 258 more ... | "large"> & Partial<...>, "slot" | ... 257 more ... | "large">': type, large
  Overload 2 of 2, '(props: StyledComponentPropsWithAs<"span", any, Props, never>): ReactElement<StyledComponentPropsWithAs<"span", any, Props, never>, string | ... 1 more ... | (new (props: any) => Component<...>)>', gave the following error.
    Type '{ children: ReactChildren; }' is missing the following properties from type 'Pick<Pick<Pick<DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, "slot" | "style" | ... 253 more ... | "is"> & { ...; } & Props, "slot" | ... 258 more ... | "large"> & Partial<...>, "slot" | ... 257 more ... | "large">': type, largets(2769)

我知道这可能是由于我在BadgeProps):但是经过几次谷歌搜索后我找不到合适的类型。

更多的是关于您的BadgeProps ,它们与Props没有任何关系。 并且由于StyledBadge具有 Props 类型的Props ,TypeScript 会大喊大叫!

为了解决这个问题,我制作了一个带有必要修复的小代码沙箱

我还对您声明Badge组件的方式进行了一些小改动:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM