Last active
September 16, 2015 07:13
-
-
Save ststeiger/869fa250be46c4ceb8c5 to your computer and use it in GitHub Desktop.
Various TypeScript constructs
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
| // No bug on jQuery and external reference $$ | |
| declare var $:any; | |
| declare var $$:any; | |
| // http://stackoverflow.com/questions/14980014/how-can-i-calculate-the-time-between-2-dates-in-typescript | |
| // var time = new Date() - new Date("2013-02-20T12:01:04.753Z"); | |
| var time = new Date().getTime() - new Date("2013-02-20T12:01:04.753Z").getTime(); | |
| // http://www.kenneth-truyers.net/2013/04/27/javascript-namespaces-and-modules/ | |
| // http://hutkev.blogspot.ch/2012/11/typescript-modules-and-namespaces.html | |
| var button : HTMLButtonElement = <HTMLButtonElement>document.createElement('button'); | |
| button.textContent = "Say Hello"; | |
| button.onclick = function() { | |
| alert(greeter.greet()); | |
| } | |
| document.body.appendChild(button); | |
| //function add(a:number, b:number) : number | |
| function add(a, b) | |
| { | |
| return a+b; | |
| } | |
| var ele:HTMLInputElement = <HTMLInputElement>document.getElementById("foobar"); | |
| var x:number = add(ele.value, 123); | |
| function passMeLongTypeDef(onSuccess:(...args: any[])=>any) | |
| { | |
| return null; | |
| } | |
| function passMe(callback:(...fargs)=> any) | |
| { | |
| } | |
| passMe(add); | |
| namespace COR | |
| { | |
| abstract class foo | |
| { | |
| protected name:string; | |
| constructor(name: string){ this.name = name;} | |
| abstract makeSound(input : string) : string; | |
| move(meters) { | |
| alert(this.name + " moved " + meters + "m."); | |
| } | |
| } | |
| class bar extends foo | |
| { | |
| constructor(name: string) { super(name); } | |
| makeSound(input : string) : string { | |
| throw new Error('This method is not yet implemented.'); | |
| } | |
| } | |
| interface Engine { | |
| getSize(): number; | |
| getTurbo(): boolean; | |
| } | |
| class StandardEngine implements Engine { | |
| constructor(private size: number, private turbo: boolean) { | |
| } | |
| public getSize(): number { | |
| return this.size; | |
| } | |
| public getTurbo(): boolean { | |
| return this.turbo; | |
| } | |
| } | |
| } | |
| // | |
| // Test class: | |
| class Greeter { | |
| greeting: string; | |
| constructor(message: string) { | |
| this.greeting = message; | |
| } | |
| greet() { | |
| return "Hello, " + this.greeting; | |
| } | |
| } | |
| var greeter = new Greeter("world"); | |
| // | |
| // Test module: | |
| module M { | |
| export class C { | |
| static count : number = 0; | |
| constructor() { | |
| C.count++; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment