简体   繁体   English

在使用typescript声明类私有变量时是否可以使用destrecturing?

[英]Is it possible to use destrcturing when declaring class private variables with typescript?

Normally when multiple variables are come from the same object I would use 通常,当多个变量来自我将使用的同一个对象时

const [ foo, bar, foobar ] = [ 'foo', 'bar', 'foobar' ]

But in type script, things like 但在类型脚本中,类似于

class Test {
    private {a,b,c} = tool;
}

It doesn't work any more. 它不再起作用了。

Is there a other way? 还有其他方法吗?

You can use Object.assign to achive the same result... 您可以使用Object.assign来获得相同的结果......

class Test {
    private a: number;
    private b: string;
    private c: boolean;

    constructor(arg: { a: number, b: string, c: boolean}) {
        Object.assign(this, arg);
    }
}

const tool = {
    a: 5,
    b: 'str',
    c: true
}

const test = new Test(tool);

console.log(JSON.stringify(tool));

The feature to destructure paramters is still active in discussion on GitHub . 在GitHub的讨论中 ,解构参数的功能仍然很活跃

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM