Created
April 22, 2020 07:48
-
-
Save kuboon/dd0ad7434c0a1b8cd94e824966195561 to your computer and use it in GitHub Desktop.
Typescript: constructor with named arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConstructorArgs { | |
namespace: string[] = ["default", "values"] | |
} | |
class Base extends ConstructorArgs { | |
constructor(opts: ConstructorArgs = new ConstructorArgs) { | |
super() | |
Object.assign(this, opts) | |
} | |
} | |
const x = new Base | |
console.log(x.namespace) // ["default", "values"] | |
const y = new Base({ namespace: ["abc"] }) | |
console.log(y.namespace) // ["abc"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment