沙箱 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):
之后放入错误的函数返回类型的结果,但是经过几次谷歌搜索之后,我找不到合适的类型。
答案 0 :(得分:0)
更多与您的BadgeProps
有关,它们与Props
毫无关系。而且由于StyledBadge
的道具类型为Props
,所以TypeScript会大喊大叫!
要解决这个问题,我做了small code sandbox with necessary fixes。
我还对声明Badge
组件的方式做了一些小改动:)