use the constructor of a class's instance to construct a new instance.
this is useful for avoiding pollution of code with new keyword along with some wonky placement of braces to make your code work.
new
class K { value: number constructor(value1: number, value2: number) { this.value = value1 + value2 }}const a = new K(1, 1)const b = constructFrom(a, 2, 2) // equivalent to `const b = new K(2, 2)`// vanilla way of constructing `const c = new K(3, 3)` using `a`const c = new (Object.getPrototypeOf(a).constructor)(3, 3)a satisfies Kb satisfies Kc satisfies K Copy
class K { value: number constructor(value1: number, value2: number) { this.value = value1 + value2 }}const a = new K(1, 1)const b = constructFrom(a, 2, 2) // equivalent to `const b = new K(2, 2)`// vanilla way of constructing `const c = new K(3, 3)` using `a`const c = new (Object.getPrototypeOf(a).constructor)(3, 3)a satisfies Kb satisfies Kc satisfies K
use the constructor of a class's instance to construct a new instance.
this is useful for avoiding pollution of code with
new
keyword along with some wonky placement of braces to make your code work.Example