Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Martin Beeby thebeebs

🏠
Working from home
View GitHub Profile
@thebeebs
thebeebs / gist:8955250
Created Feb 12, 2014
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.
View gist:8955250
//
interface Thing {
a: number;
b: string;
c: boolean;
}
function process(x: Thing){
return x.a;
@thebeebs
thebeebs / Vanilla JS
Last active Aug 29, 2015
An ordinary function in JavaScript.
View Vanilla JS
function process(x){
x.name = "foo";
var v = x + x;
alert(v);
}
@thebeebs
thebeebs / IntroduceTypescript
Created Feb 12, 2014
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.
View IntroduceTypescript
function process(x: string){
x.name = "foo";
var v = x + x;
alert(v);
}
@thebeebs
thebeebs / number.ts
Created Feb 12, 2014
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
View number.ts
function process(x : number){
var v = x + x;
alert(v);
}
@thebeebs
thebeebs / boolean.ts
Created Feb 12, 2014
Lastly we will add the last primitive type Boolean, this will error since you can't add two Booleans together.
View boolean.ts
function process(x: boolean){
var v = x + x;
}
@thebeebs
thebeebs / stringArray.ts
Created Feb 12, 2014
You can have an array of string. This means IDEs and Text editors can add statement completion to an array
View stringArray.ts
function process(x: string[]){
x[0].toLowerCase();
}
@thebeebs
thebeebs / stringFunction.ts
Created Feb 12, 2014
You can state that x is a fucntion That returns a string. If you call x the result will be a string.
View stringFunction.ts
function process(x: () => string){
x().toLowerCase();
}
@thebeebs
thebeebs / objectLiteral.ts
Created Feb 12, 2014
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.
View objectLiteral.ts
function process(x: {a: string; b: number}){
return x.a.length;
}
@thebeebs
thebeebs / gist:8964110
Created Feb 12, 2014
If you add an interface you can use that as a type.
View gist:8964110
interface Thing {
a: number;
b: string;
}
function process(x: Thing){
return x.b.length;
}
@thebeebs
thebeebs / typeInference.ts
Last active Aug 29, 2015
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.
View typeInference.ts
interface Thing {
a: number;
b: string;
}
function process(x: Thing){
return x.a;
}
var n = process({a: 10, b:'Beeby'})
You can’t perform that action at this time.