Skip to content

Instantly share code, notes, and snippets.

@ststeiger
Last active September 16, 2015 07:13
Show Gist options
  • Select an option

  • Save ststeiger/869fa250be46c4ceb8c5 to your computer and use it in GitHub Desktop.

Select an option

Save ststeiger/869fa250be46c4ceb8c5 to your computer and use it in GitHub Desktop.
Various TypeScript constructs
// 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