Skip to content

super() with an options object (possibly a FAQ) #7644

@elibarzilay

Description

@elibarzilay

(Apologies if this is known, but I search a bunch of places and didn't find anything.)

Is there a good way to deal with a constructor of a derived class that is getting an options kind of value that needs to be merged with a defaults value? I started with something like this:

export class Foo extends Bar {
    ...
    private options: any = {};
    ...
    constructor(x: Something, y: SomethingElse, options: any = {}) {
        this.options = $.extend({}, defaults, options);
        super(this.options.blah);
        ...
    }
}

This failed and I eventually resorted to:

    constructor(x: Something, y: SomethingElse, options: any = {}) {
        super(options.blah || defaults.blah);
        this.options = $.extend({}, defaults, options);
        ...
    }

which is duplicating functionality (and sloppy).

Hopefully I'm not the only one who ran into this problem and there's good way to do that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions