Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kuboon
Created April 22, 2020 07:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuboon/dd0ad7434c0a1b8cd94e824966195561 to your computer and use it in GitHub Desktop.
Save kuboon/dd0ad7434c0a1b8cd94e824966195561 to your computer and use it in GitHub Desktop.
Typescript: constructor with named arguments
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