Destructured parameter properties in constructor

What about:

class Destructed {
    private prefix: string;
    private suffix: string;

    constructor(opts: DestructedOptions) {
        Object.assign(this, opts);
    }
}

Also, there’s an open issue on this: Combining destructuring with parameter properties


Edit

If you want to avoid re-writing the properties in the class then you’ll need to make the members public, when that’s the case then the solution is as shown in the issue I linked to:

class Destructed {
    constructor(opts: DestructedOptions) {
        Object.assign(this, opts);
    }
}

interface Destructed extends DestructedOptions { }

let destructed = new Destructed({ prefix: "prefix", suffix: "suffix" });
console.log(destructed.prefix);
console.log(destructed.suffix);
console.log(destructed.DoesntExist); // error

(code in playground)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)