liberty-prettydiff
Version:
Language aware code comparison tool for several web based languages. It also beautifies, minifies, and a few other things.
166 lines (128 loc) • 3.52 kB
Plain Text
let array: number[] = [1, 2, 3.14, 42];
let theAnswer: number = array[3]; // 42
let offTheEnd: number = array[100]; // No error
let array2: Array<string> = ["an alternate", "syntax", "for arrays"];
let tuple: [string, number, boolean] = ["foo", 0, true];
let array : number[] = [
1, 2, 3.14, 42
],
theAnswer: number = array[3], // 42
offTheEnd: number = array[100], // No error
array2 : Array<string> = [
"an alternate", "syntax", "for arrays"
],
tuple : [string, number, boolean] = ["foo", 0, true];
// Indexing into the array will return the type at a given index.
(tuple[0] : string);
(tuple[1] : string);
// Indexing into an statically unknown index will return a general type.
declare var unknownNumber: number;
// `void` is none of `string`, `number`, or `boolean`
(tuple[unknownNumber] : void);
(tuple[unknownNumber] : string | number | boolean); // OK
// Values written must be compatible with the type at that index.
tuple[1] = -1;
tuple[0] = false;
let object: {
bar: number,
foo: string
} = {
bar: 0,
foo: "foo"
};
(object.foo : string);
// Property writes must be compatible with the declared type.
object.bar = "bar";
let coolRating: {
[id: string]: number
} = {};
coolRating["sam"] = 10; // Yes, it's a 0-10 scale.
function makeCallable(): {
(x : number): string;
foo : number
} {
function callable(number) {
return number.toFixed(2);
}
callable.foo = 123;
return callable;
};
var callable = makeCallable();
var callableReturn: string = callable(Math.PI); // "3.14"
var callableFoo: number = callable.foo; // 123
function greatestCommonDivisor(a : number, b : number): number {
if (!b) {
return a;
}
return greatestCommonDivisor(b, a % b);
}
// Annotations included for example purposes only.
[1, 2, 3].map((num : number) : number => num * 2);
async function getFriendNames(friendIDs : Promise<number[]>, getFriendName : (id : number) => Promise<string>,): Promise<string[]> {
var ids = await friendIDs;
var names = await Promise.all(ids.map(getFriendName));
return names;
}
function * infinity(): Generator<number, void, void> {
var n = 0;
while (true) {
yield n += 1;
}
}
class MyClass {
foo : string;
constructor(foo : string) {
this.foo = foo;
}
bar(): string {
return this.foo;
}
}
var myInstance: MyClass = new MyClass("foo");
(myInstance.foo : string);
(myInstance.bar() : string);
interface Fooable {
foo(): string;
}
class AFoo {
foo() {
return "foo from A";
};
}
var x = class BFoo {
foo() {
return "foo from B";
};
};
(new AFoo : Fooable);
(new BFoo : Fooable);
function DietClass(foo : string) {
this.foo = foo;
}
DietClass.prototype.bar = function () {
return this.foo;
};
var myDietInstance: DietClass = new DietClass("foo");
(myDietInstance.foo : string);
(myDietInstance.bar() : string);
type GenericObject<T> = {
foo: T
};
var numberObject: GenericObject<number> = {
foo: 0
};
var stringObject: GenericObject<string> = {
foo: "foo"
};
class GenericClass<T> {
x: T;
constructor(x : T) {
this.x = x;
}
}
var numberInstance: GenericClass<number> = new GenericClass(0);
var stringInstance: GenericClass<string> = new GenericClass("");
function findMax<T>(arr : T[], compare : (a : T, b : T) => number) {
var sorted = arr.sort(compare);
return sorted[sorted.length - 1];
};