当您解构一个对象时,如何输入展开的“其余”属性,如下所示:
const foo = ({ a, b, ...restOfCToZ }) => // ...
在我的具体情况下,我有一个 React 组件,它会解构
isTwoColumn
道具以及所有其他道具:
interface FieldProps {
isTwoColumn?: boolean;
props: ? how do I define the "rest of" props ? ;
}
export const Field = ({ isTwoColumn, ...props } : FieldProps ) => (
<div
{...addClassToProps(props, isTwoColumn ? ' two-column' : '' )}
/>
);
我的
addClassToProps
函数需要 { className: string, ...props: something? }
的属性,所以最终我尝试输入组件的 props
,但实际上我只是想了解如何在对象上输入解构的“其余”属性一般来说。
{ a, b, ...props } = {a: "a", b: "b", c: "c", d: "d", e: "e"}
console.log(props) // => (Object(c: "c", d: "d", e: "e"))