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:8964901
Created February 12, 2014 21:29
Interfaces can model all sorts of JavaScript objects, You can add a function signature simply. foo accepts a string and a number and returns a string
interface Thing {
a: number;
b: string;
foo(x: string, n: number): string;
}
function process(x: Thing){
return x.foo("string",10);
}
@thebeebs
thebeebs / optionalParameter.ts
Created February 12, 2014 21:24
You can make (c) optional by postfixing it with a question mark. This means it's still there but it is not required. If you do try and pass C it must be of type boolean. Therefore the param is optional however the type is not.
interface Thing {
a: number;
b: string;
c?: boolean;
}
function process(x: Thing){
return x.a;
}
@thebeebs
thebeebs / invalidInterface
Created February 12, 2014 21:16
If we introduce a property (c) but then do not provide it when we initialise the process function. We will encouter a complie time error.
interface Thing {
a: number;
b: string;
c: boolean;
}
function process(x: Thing){
return x.a;
}
@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'})
@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 / 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 / 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 / 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 / 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 / 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);
}