簡體   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