Created
March 28, 2020 20:59
-
-
Save jlopezr/4f83b54845b2ec3af854e672853d313f to your computer and use it in GitHub Desktop.
Example of a generic controller in TS
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
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