Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 类型';字符串';不可分配给类型';未定义';_Reactjs_Typescript - Fatal编程技术网

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
?干杯!以前不知道
省略
。肯定会用很多!下面是一些可能很有用的实用程序类型: