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 / 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 / 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 / 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 / opFunctions.ts
Created February 12, 2014 21:34
Functions can have optional parameters too
interface Thing {
a: number;
b: string;
foo(x: string, n?: number): string;
}
function process(x: Thing){
return x.foo("string");
}
@thebeebs
thebeebs / overloads.ts
Created February 12, 2014 21:39
You can have overloads
interface Thing {
a: number;
b: string;
foo(x: string): string;
foo(x: number): string;
}
function process(x: Thing){
return x.foo(2)
}
@thebeebs
thebeebs / realWorld.ts
Last active August 29, 2015 13:56
Modeling Real world JavaScript objects can be complex. Here we have added the overloads with a slightly different syntax, and have added a data object to foo.
interface Thing {
a: number;
b: string;
foo: {
(x: string): string;
(x: number): number;
data: any;
};
}
@thebeebs
thebeebs / constructorsAndIndexes.ts
Last active August 29, 2015 13:56
You can model constructors, You can model what happen when someone indexes the (thing) telling TypeScript to return a date.
interface Thing {
a: number;
b: string;
foo: {
(x: string): string;
(x: number): number;
data: any;
};
new (s: string): Element;
[index: number]: Date;
@thebeebs
thebeebs / objectInterface.ts
Created February 12, 2014 22:09
You can add an interface to an object so the miss spelling of clear() is picked up and the app will not compile.
interface Rapper {
say(x: string): void;
clear(): void;
speak(): string;
}
function makeRapper(): Rapper{
var words;
return {
say: function (x: string){
@thebeebs
thebeebs / definitionFiles.ts
Last active August 29, 2015 13:56
TypeScript can infer meaning about the (e) parameter because of the lib.t.ds file that provides an 8000 line interface for the entire javascript runtime.
window.onmousemove = function(e) {e.clientX};
@thebeebs
thebeebs / designer.html
Created November 7, 2014 16:17
designer
<link href="../core-icon-button/core-icon-button.html" rel="import">
<link href="../core-toolbar/core-toolbar.html" rel="import">
<link href="../core-header-panel/core-header-panel.html" rel="import">
<link href="../topeka-elements/category-images.html" rel="import">
<link href="../core-icon/core-icon.html" rel="import">
<link href="../core-icons/core-icons.html" rel="import">
<link href="../core-icons/av-icons.html" rel="import">
<link href="../paper-fab/paper-fab.html" rel="import">
<polymer-element name="my-element">