Skip to content

Instantly share code, notes, and snippets.

@jlopezr
Created March 28, 2020 20:59
Show Gist options
  • Save jlopezr/4f83b54845b2ec3af854e672853d313f to your computer and use it in GitHub Desktop.
Save jlopezr/4f83b54845b2ec3af854e672853d313f to your computer and use it in GitHub Desktop.
Example of a generic controller in TS
const message: string = 'hello world';
console.log(message);
class Person {
public name!: string;
public surname!: string;
public say() { console.log("person");}
}
class Car {
public maker!: string;
public model!: string;
public say() { console.log("car");}
}
type Instantiable = {new(...args: any[]): any};
class Factory {
constructor(private model: Instantiable) {
console.log("Creating factory")
}
build<T>(): T {
return new this.model();
}
}
const f1 = new Factory(Person);
const f2 = new Factory(Car);
var p: Person = f1.build();
p.say();
var c: Car = f2.build();
c.say();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment