Reactjs 类型';字符串';不可分配给类型';未定义';
鉴于这些类型:Reactjs 类型';字符串';不可分配给类型';未定义';,reactjs,typescript,Reactjs,Typescript,鉴于这些类型: export type ButtonProps = { kind?: 'normal' | 'flat' | 'primary'; negative?: boolean; size?: 'small' | 'big'; spinner?: boolean; } export type LinkButtonPropsExtended = ButtonProps & React.HTMLProps<HTMLAnchorElement&g
export type ButtonProps = {
kind?: 'normal' | 'flat' | 'primary';
negative?: boolean;
size?: 'small' | 'big';
spinner?: boolean;
}
export type LinkButtonPropsExtended = ButtonProps & React.HTMLProps<HTMLAnchorElement>;
const LinkButton = ({ children, href, ...rest }: LinkButtonPropsExtended) => (
<a href={href} className={cls(rest)}>
{ children }
</a>
);
导出类型按钮操作={
种类?:“普通”|“扁平”|“初级”;
负?:布尔值;
大小?:“小”|“大”;
微调器?:布尔型;
}
导出类型LinkButtonPropsExtend=ButtonProps&React.HTMLProps;
常量链接按钮=({children,href,…rest}:LinkButtonPropsExtend)=>(
);
这个用例是如何产生的:
<LinkButton href={url} size="big">My button</LinkButton>
我的按钮
引发此类型错误:
64:53类型“string”不能分配给类型“undefined”。
63 |另一个按钮
>64 |我的按钮
| ^
Typescript编译器是否将
大小
道具解释为未定义的类型
?为什么?问题在于React.HTMLProps
已经具有size
属性,声明如下:
size?: number
这会导致大小
类型为未定义
。下面是一个示例,您可以看到类型是如何解析为未定义的
您最好更改size
属性的名称或省略HTMLProps
size
属性,例如:
type MyHTMLProps = Omit<React.HTMLProps<HTMLAnchorElement>, 'size'>;
export type LinkButtonPropsExtended = ButtonProps & MyHTMLProps;
键入MyHTMLProps=Omit;
导出类型LinkButtonPropsExtend=ButtonProps&MyHTMLProps;
请提供typescript
版本。我将尝试在沙箱中重现这一点。什么是cls
?干杯!以前不知道省略。肯定会用很多!下面是一些可能很有用的实用程序类型: