Last active
August 30, 2017 18:49
-
-
Save JohnL4/5e017d47e92a758b8d8722a88614b48d to your computer and use it in GitHub Desktop.
Simple horsing around to demonstrate what TypeScript does (paste into playground at typescriptlang.org)
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
// ============================================================================================ | |
// Typescript is Javascript annotated with type info. | |
// "Compilation" of Typescript is (in the simple case) | |
// stripping out the type annotations. | |
var someNumber: number; | |
someNumber = 2; | |
someNumber = "bad"; // Type mismatch | |
// A slightly fancy way of telling Typescript that this variable is | |
// defined elsewhere (e.g., in another file or SCRIPT element, | |
// but has a certain type). Note that the compiler doesn't even include | |
// this comment. Magic! | |
declare var fromWhere: string; | |
fromWhere = "REVIEWTAB"; // Typescript compiler is happy. | |
/** | |
* Utility function for this demo to write the given message | |
* in a PRE element of the document. | |
*/ | |
function scribble(aMsg: string): void { | |
console.log(aMsg); | |
// Get a reference to the document's PRE element, or null if there isn't one. | |
var preElt = document.body.querySelector("pre"); | |
if (preElt) { | |
// Do nothing, we're good | |
// console.log("got preElt"); | |
} | |
else { | |
// console.log('make new pre elt'); | |
preElt = document.createElement("pre"); | |
// console.log('append pre elt to document body'); | |
document.body.appendChild(preElt); | |
} | |
// console.log('append new Text to pre elt'); | |
preElt.appendChild(new Text(aMsg + '\n')); | |
} | |
/** | |
* A simple numeric function. | |
*/ | |
function sqr(x: number): number | |
{ | |
return x * x; | |
} | |
someNumber = sqr(3); | |
scribble(`3 squared is ${someNumber}`); | |
someNumber = sqr("this won't end well"); | |
scribble(`a string squared is ${someNumber}`); | |
// -------------------------------------------------------------------------------------------- | |
/** | |
* For the more bold, some classes and object-oriented programming. | |
* The Typescript compiler turns this into idiomatic Javascript that gives | |
* O-O behavior (including the __extends() function you see at the top | |
* of the output). | |
*/ | |
abstract class Base { | |
/** | |
* Read-only name. | |
*/ | |
public get name(): string { return this._name }; | |
public constructor(private _name: string) { } | |
public toString(): string { | |
let t = this.constructor.name; | |
return `${t} object named ${this.name}`; | |
} | |
} | |
class SubClass1 extends Base { | |
public get subId(): number { return this._subId } | |
public constructor(aName: string, private _subId: number) { | |
super(aName); | |
} | |
public toString(): string { | |
let t = this.constructor.name; | |
return `${t} object named ${this.name} having subId ${this.subId}`; | |
} | |
} | |
class SubClass2 extends Base { | |
public get value(): number { return this._value } | |
public set value(aValue: number) { this._value = aValue; } | |
public constructor(aName: string, private _value: number) { | |
super(aName); | |
} | |
public toString(): string { | |
let t = this.constructor.name; | |
return `${t} object named ${this.name} having value ${this.value}`; | |
} | |
} | |
// ------------------------- class declarations end ------------------------------ | |
scribble(""); | |
let subObj1: SubClass1; | |
subObj1 = new SubClass2("Morty", 33.8); // Type mismatch | |
// Who knows how this will turn out? Once you have a type mismatch, | |
// all bets are off because you're back in the wild land of Javascript. | |
scribble(`Created ${subObj1.toString()}`); | |
// Let the polymorphism commence! | |
let obj: Base; | |
obj = new Base("Zed"); | |
scribble(`Created ${obj.toString()}`); | |
obj = new SubClass1("Alice", 12); | |
scribble(`Created ${obj.toString()}`); | |
obj = new SubClass2("Bob", 3.14); | |
scribble(`Created ${obj.toString()}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment