This file contains 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
class PrivateBox { | |
constructor(private width: number, private height: number) {} | |
area() { return this.width * this.height; } | |
} | |
let x = new PrivateBox(4, 5); | |
console.log(x.area()); // OK | |
console.log(x.width); // ERROR: 'width' is private and only | |
// accessible within class 'PrivateBox'. |
This file contains 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
interface IBox { | |
readonly width: number; | |
readonly height: number; | |
} | |
interface IArea { | |
readonly area: number; | |
} |
This file contains 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
// Type parameters can have default values, | |
// so `var t: BTree` means `var t: BTree<any,any>` | |
export class BTree<K=any, V=any> | |
{ | |
// Root node (key-value pairs are stored in here) | |
private _root: BNode<K, V>; | |
// Total number of items in the collection | |
_size: number = 0; | |
// Maximum number of items in a single node | |
_maxNodeSize: number; |
This file contains 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 whatAmI(thing: string|RegExp|null|number[]|Date) { | |
if (thing instanceof RegExp || typeof thing === "string") { | |
// Here, TypeScript knows that thing is RegExp|string | |
if (typeof thing !== "string") | |
console.log("Ahh, so it's a RegExp: " + thing.toString()); | |
else | |
console.log("It's a string of length " + thing.length); | |
} else { | |
// Here, TypeScript knows that thing is null|number|Date | |
if (thing == null) |
This file contains 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
<!doctype html> | |
<html> | |
<head> | |
<style> | |
p { font-family: serif; } | |
table { | |
border-collapse:collapse; | |
} | |
table td, table td { | |
vertical-align: top; |
OlderNewer