Created
June 19, 2014 06:17
-
-
Save jchadwick/675e4ffbe27dc63df066 to your computer and use it in GitHub Desktop.
TypeScript demo
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
declare var log: { | |
(value): void; | |
}; | |
interface Food { | |
name: string; | |
ingredients?: string[]; | |
} | |
enum AnimalType { | |
Unknown = 0, | |
Dog = 1, | |
Cat = 2, | |
} | |
class Animal { | |
public name: string; | |
constructor(name: string) { | |
this.name = name; | |
} | |
eat(food: Food); | |
eat(food: string); | |
eat(food: any) { | |
var foodName = (typeof food == 'string') ? food : food.name; | |
log([this.name, 'eats', foodName || 'NOTHING'].join(' ')); | |
} | |
speak() { | |
log([this.name, "doesn't know how to speak!"].join(' ')); | |
} | |
static create(type: AnimalType.Dog, name: string): Dog; | |
static create(type: AnimalType, name: string): any { | |
if (type == AnimalType.Dog) { | |
return new Dog(name); | |
} | |
else { | |
return new Animal(name); | |
} | |
} | |
} | |
class Dog extends Animal { | |
constructor(name: string) { | |
super(name); | |
} | |
speak() { | |
log([this.name, 'says "WOOF!"'].join(' ')); | |
} | |
fetch<T>(thing: () => T): T { | |
var fetched = thing(); | |
log([this.name, 'fetched', fetched].join(' ')); | |
return fetched; | |
} | |
say(value: string) { | |
log([this.name, 'says', value].join(' ')); | |
} | |
} | |
class Util { | |
FindElement(id: string, tag: 'div'): HTMLDivElement; | |
FindElement(id: string, tag: 'span'): HTMLSpanElement; | |
FindElement(id: string, tag: string): HTMLElement; | |
FindElement(id: string, tag: any): HTMLElement { | |
return document.getElementById(id); | |
} | |
} | |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<meta name="description" content="Demo of the TypeScript language" /> | |
<title>TypeScript HTML App</title> | |
<style> | |
body { | |
font-size: x-large; | |
} | |
</style> | |
<script src="util.js"></script> | |
<script src="Animal.js"></script> | |
<script src="app.js"></script> | |
</head> | |
<body> | |
</body> | |
</html> |
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
function log(value) { | |
document.writeln([new Date().toLocaleTimeString(),': ', value, '<br>'].join('')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment