Created
February 27, 2016 15:15
-
-
Save PeterOrneholm/475b476244f99bc93c30 to your computer and use it in GitHub Desktop.
Example of simple TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumAndDisplay(left : number, right : number, prefix : string = "Result") { | |
var result = left + right; | |
alert(`${prefix}: ${result}`); | |
} | |
sumAndDisplay(1, 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function filter<T>(items : T[], predicate : (x : T) => boolean) : T[] { | |
var filtered = []; | |
for(let x of items){ | |
if(predicate(x)){ | |
filtered.push(x); | |
} | |
} | |
return filtered; | |
} | |
interface IPerson { | |
active : boolean; | |
name: string; | |
} | |
let people : IPerson[] = [ | |
{ active: true, name: "Peter" }, | |
{ active: false, name: "Olle" }, | |
{ active: true, name: "Isabelle" } | |
]; | |
let activePeople = filter(people, p => p.active); | |
alert(activePeople.length); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment