解构对象参数

问题描述 投票:0回答:1

当您解构一个对象时,如何输入展开的“其余”属性,如下所示:

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
,但实际上我只是想了解如何在对象上输入解构的“其余”属性一般来说。

reactjs typescript destructuring
1个回答
-1
投票

{ a, b, ...props } = {a: "a", b: "b", c: "c", d: "d", e: "e"}

console.log(props) // => (Object(c: "c", d: "d", e: "e"))

© www.soinside.com 2019 - 2024. All rights reserved.