Skip to content

Instantly share code, notes, and snippets.

@xaliphostes
Created February 1, 2023 14:38
Show Gist options
  • Save xaliphostes/b6c3fe9174ba17b839b87af7898df30e to your computer and use it in GitHub Desktop.
Save xaliphostes/b6c3fe9174ba17b839b87af7898df30e to your computer and use it in GitHub Desktop.
Create an object instance
// --------------------------------------------------------
const create = (Type: any, params: any) => new Type(params)
// --------------------------------------------------------
// Example
class A {
private a = 0
private b = 0
constructor({a: number, b: number}) {
this.a = a
this.b = b
}
display() {
console.log(this.a, this.b)
}
}
const a = create(A, {a: 0,b: 1})
a.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment