Javascript 如何使用TypeScript中的对象分解为构造函数中的类级属性赋值?

Javascript 如何使用TypeScript中的对象分解为构造函数中的类级属性赋值?,javascript,reactjs,typescript,object-destructuring,Javascript,Reactjs,Typescript,Object Destructuring,我正在为我的react项目使用Typescript。因此,我在构造函数中传递道具。 我想打破构造函数中的“道具”,并将其分配给类级变量 这是我的密码- abstract class IProps{ abstract type:'text'|'checkbox'|'radio'; abstract value?: any; abstract name?: string; abstract id?:string; abstract onClick?: ((event: React.MouseEven

我正在为我的react项目使用Typescript。因此,我在构造函数中传递道具。 我想打破构造函数中的“道具”,并将其分配给类级变量

这是我的密码-

abstract class IProps{
abstract type:'text'|'checkbox'|'radio';
abstract value?: any;
abstract name?: string;
abstract id?:string;
abstract onClick?: ((event: React.MouseEvent<HTMLInputElement, MouseEvent>) => void);
abstract onChange?:((val: any) => void);
abstract classnames: string;
abstract placeholder?: any;
}
interface InputProps extends IProps{
    isvalid?: boolean;
}

export class Input extends Component<InputProps>{

    isvalid: boolean|undefined=true;
iProps: IProps|null = null;

constructor(props: InputProps){
    super(props);
    console.log(this.props);
    const {isvalid, ...iProps} = this.props;
    this.isvalid = isvalid;
    this.iProps = iProps;        
    }
}
但是我想在不使用
const
语句的情况下赋值。我想做这样的事情-

{isvalid: this.isvalid, ...iProps: this.iProps} = this.props;

如何分配这样的值?

因为它以一个
{
开头,您需要通过将它包装在
()
中来告诉解析器它是一个表达式,而不是块的开头:

-


因此,每次我想在语句中使用{}时,我都必须将它括在括号中?@PrafullDhadkar-如果
{
在语句的开头,则是。否则,它将启动一个块。当解析器需要表达式(
a={}
)时,这不是真的,但当解析器需要表达式或语句时,这是正确的。
()
告诉解析器您正在使用表达式,而不是启动块。
…this.iProps
位非常有趣。只看到使用的变量收集剩余的属性。从不使用属性访问器。因此,
({key,…this.a.b.c}=obj)
this.a.b.c=objButWithoutKey
@adiga-Right,加上
key=obj.key;
。解构表达式中的目标(
{source:target}
,或
[target]
用于iterable解构)可以是任何可赋值的东西,包括对象属性。
{isvalid: this.isvalid, ...iProps: this.iProps} = this.props;
({isvalid: this.isvalid, ...this.iProps} = this.props);
class Example extends React.Component<InputProps> {
  isvalid: boolean;
  iProps: Partial<InputProps> = {}; // <=== Without the `= {}`, the assignment in
                                    // the constructor gets what seems to be an
                                    // erroneous error about trying to use iProps
                                    // before it's initialized
  constructor(props: InputProps){
    super(props);
    ({isvalid: this.isvalid, ...this.iProps} = this.props);
  }
}