Skip to content

Instantly share code, notes, and snippets.

View thebeebs's full-sized avatar
🏠
Working from home

Martin Beeby thebeebs

🏠
Working from home
View GitHub Profile
@thebeebs
thebeebs / gist:8955250
Created February 12, 2014 13:07
You can define you own types using interfaces Thing describes and object literal with an a number property and a b string property. Even though the process function dosen't return a type We have given the compiler enogh information to infer that n at the bottom will be of type number.
//
interface Thing {
a: number;
b: string;
c: boolean;
}
function process(x: Thing){
return x.a;
@thebeebs
thebeebs / Vanilla JS
Last active August 29, 2015 13:56
An ordinary function in JavaScript.
function process(x){
x.name = "foo";
var v = x + x;
alert(v);
}
@thebeebs
thebeebs / IntroduceTypescript
Created February 12, 2014 18:41
If I annotate x as a string things start to light up In sublime. You will nottice errors being thrown in the code below it is saying that a string can't have a name property.
function process(x: string){
x.name = "foo";
var v = x + x;
alert(v);
}
@thebeebs
thebeebs / number.ts
Created February 12, 2014 19:09
So If I change the x to a number. Then the product of x+x will be a number. So v is inferred to be a number
function process(x : number){
var v = x + x;
alert(v);
}
@thebeebs
thebeebs / boolean.ts
Created February 12, 2014 19:24
Lastly we will add the last primitive type Boolean, this will error since you can't add two Booleans together.
function process(x: boolean){
var v = x + x;
}
@thebeebs
thebeebs / stringArray.ts
Created February 12, 2014 19:36
You can have an array of string. This means IDEs and Text editors can add statement completion to an array
function process(x: string[]){
x[0].toLowerCase();
}
@thebeebs
thebeebs / stringFunction.ts
Created February 12, 2014 20:31
You can state that x is a fucntion That returns a string. If you call x the result will be a string.
function process(x: () => string){
x().toLowerCase();
}
@thebeebs
thebeebs / objectLiteral.ts
Created February 12, 2014 20:36
You can state that x is a function you can describe the shape of an object so in this instance a will have a string propert called a and a b property of type number.
function process(x: {a: string; b: number}){
return x.a.length;
}
@thebeebs
thebeebs / gist:8964110
Created February 12, 2014 20:43
If you add an interface you can use that as a type.
interface Thing {
a: number;
b: string;
}
function process(x: Thing){
return x.b.length;
}
@thebeebs
thebeebs / typeInference.ts
Last active August 29, 2015 13:56
You can define you own types using interfaces. Thing describes the object literal with an (a) number property and a (b) string property. Even though the process function doesn't return a type we have given the compiler enough information to infer that (n) will be of type number.
interface Thing {
a: number;
b: string;
}
function process(x: Thing){
return x.a;
}
var n = process({a: 10, b:'Beeby'})