Last active
July 6, 2019 19:02
-
-
Save samwx/f1ecbf2ee1e6d6cba832deefc12392ba to your computer and use it in GitHub Desktop.
Typescript examples
This file contains hidden or 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
function SelfDriving(classFn: Function) { | |
classFn.prototype.selfDrivable = true; | |
} | |
@SelfDriving | |
class SomeCar { | |
private make: string; | |
constructor(make: string) { | |
this.make = make; | |
} | |
} |
This file contains hidden or 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
function Wheels(numberOfWheels: number) { | |
return function(classFn: Function) { | |
classFn.prototype.wheels = numberOfWheels; | |
} | |
} | |
@Wheels(3) | |
class ExoticCar { | |
private make: string; | |
constructor(make: string) { | |
this.make = make; | |
} | |
} |
This file contains hidden or 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
export class MailSender<T> { | |
send(mail: T) { | |
console.log('Sending...', mail); | |
} | |
} | |
const billing = new MailSender<CardBilling>(); | |
billing.send(new CardBilling()); |
This file contains hidden or 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
interface IAnimal { | |
name: string; | |
sound: () => string; | |
run: () => number; | |
} | |
class Dog implements IAnimal { | |
name: string; | |
constructor(name: string) { | |
this.name = name; | |
} | |
run() { | |
return 30; | |
} | |
sound() { | |
return 'auau'; | |
} | |
} |
This file contains hidden or 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
type Person = { | |
firstName: string; | |
lastName: string; | |
country: string; | |
}; | |
function f(): Person { | |
return { | |
firstName: 'Samuel', | |
lastName: 'Martins', | |
country: 'Brasil', | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment