Created by Bryan E Rayner
Javascript, for all it's benefits, provides a considerable challenge for many developers.
There are many languages which compile to Javascript:
Today, we'll be focusing on Typescript.
Typescript is a language which compiles to well-written Javascript.
// Typescript
class Welcomer
{
message:string = '';
setMessage(newMessage:string) {
this.message = newMessage;
}
sayMessage() {
console.log(this.message);
}
}
//Javascript
var Welcomer = (function () {
function Welcomer() {
this.message = '';
}
Welcomer.prototype.setMessage = function (newMessage) {
this.message = newMessage;
};
Welcomer.prototype.sayMessage = function () {
console.log(this.message);
};
return Welcomer;
})();
All Javascript is valid Typescript
// Valid Typescript:
var hello = function(){
return "Hey y'all!";
};
var object = {
normal:'property',
definitions:'you bet'
};
console.log(hello());
Typescript compiles ES6 syntax to ES5, and ES3
// Fat arrow functions:
var boundLookup = (propName) => this[propName];
// Splat operator
var splattedFunction = function(one, two, ...rest){
// rest is the rest of the arguments after two
}
Typescript optionally gives developers strong typing.
// Without types
var foo = 'five';
foo = 5; // What if you don't catch this bug?
// With types
var bar: string = 'six';
bar = 6; // This will throw an error in the compiler.
Type systems help you help yourself
Type systems tell computers what the heck you're doing.
They help translate this:
var boxCounter = 5;
To this value in RAM:
00000101
As opposed to this ASCII value:
00110101
Here's Javascript and C++ in a quick comparison
// Javascript:
var counter; // Allocate an arbitrary amount of RAM.
counter = 5; // Store a value of 5.
counter = 'cinco'; // Store an array of 16-bit Unicode Characters.
// C++:
long counter; // Allocate 64 bits of RAM.
counter = 5; // Store a value of 5
counter = 'no bueno'; // This code won't even work
// Perfectly valid (and insane) Javascript:
var object = {};
var summary = 'The fruit bowl has ';
object.value = function () {
return summary + 'apple';
}
object['value'] = object.value() + "s and oranges.";
object.value = (function() {
return object.value + ' And bananas.';
})();
summary = (function() {console.log(object.value);})();
// Outputs: 'The fruit bowl has apples and oranges. And bananas.
Interfaces tell IDE's (and Typescript) what to expect
// Duck interface
interface Duck {
age: number;
name: string;
secretIdentity: string;
walk(distance:number): void;
fly(destination:string) : void;
quack(volume: number): void;
}
Sort of like free documentation!
For a typical SPA, we might see something like this:
interface Resource<T> {
route: string;
get(): T;
post(value:T): boolean;
put(values: any): boolean;
delete(value: T): boolean;
}
<T> represents a generic type. It gets defined as you use it.
interface Chicken{
id: number;
name: string;
breed: string;
weight: number;
}
class ChickenResource implements Resource<Chicken>
{
route: string;
get(): Chicken;
post(value:Chicken): boolean;
put(values: any): boolean;
delete(value: Chicken): boolean;
}
Let's implement a Graph API in Typescript
Graph's are a data structure which model connections
interface INode<T>
{
// Returns a unique identifier
_getUId:()=>string;
// Returns the node data
_getData:()=> T;
}
interface IGraph<T>
{
_uid:string;
nodesCount:number;
addNode(newNode:INode<T>): void;
addEdge(v: INode<T>, w: INode<T>): void;
getAdjacentNodes(v: INode<T>): INode<T>[];
countVertices(): number;
getNode(id:string): INode<T>;
//... Plus more goodies...
}
Concordance of Ephesians
Typescript adds incredible benefit to team projects:
For this demonstration, we'll add some functionality to a Graph API.