The future today! and more...
CoffeeScript Dart Haxe Opa
var x: string;
var y: number;
var z: boolean;
var foo: any;
var bar; // Same as "any"
var x;
var y;
var z;
var foo;
var bar;
var a: any[];
var b: string[];
var p: Product[];
var a;
var b;
var p;
function calcPrice(products: Products[]):number {
.
.
.
}
function calcPrice(products) {
.
.
.
}
var func : (name:string) => number;
function process(x: () => string){
x().toLowerCase();
}
var func;
function process(x){
x().toLowerCase();
}function process(x: {a:string; b:number}){
return x.a.length;
}interface IThing {
a: number;
b: string;
}
function process(x: IThing){
return x.a.length;
}
function process(x){
return x.a.length;
}
function process(x){
return x.a.length;
}
interface IProduct {
name : string;
price : number;
}
function hasName(product: IProduct){
return product.name.length > 0;
}
var isNamed = hasName({name:'iPhone', price:1000});
function hasName(product){
return product.name.length > 0;
}
var isNamed = hasName({name:'iPhone', price:1000});interface IPerson {
age : number;
name : string;
address? : string; // <-- optional field
}
function getName(p: IPerson){
return p.name;
}
var name = getName({age:10, name:'Me'});
interface IPerson {
age : number;
name : string;
address : string;
walk(distance:number): number; // <-- a Function
}
interface IPerson {
age : number;
name : string;
address : string;
walk(distance:number): number;
walk(destination:string): number;
walk(location:{x:number, y:number}): number;
}
interface IPerson {
age : number;
name : string;
address : string;
walk(distance:number): number;
(): string; // <-- a Function
}var x = 3; // x is a number
class MyClass {
name = "Foo"; // name is a string
}
function foo(value = false) { // value is a boolean
}
function calc() {
return 55; // calc returns a number
}
var x = calc(); // x is also a number
interface IHuman {
age: number;
walk(distance:number):void;
}
var man : IHuman = {
age : 120,
walk: function(distance) {
console.log(distance); // distance inferred to be a number
}
}
window.onmousedown = function(mouseEvent) {
// mouseEvent inferred as MouseEvent
console.log(mouseEvent.button);
};
var x = 3; // x is a number
x = "45"; // compiler error
var foo = {};
foo.description = 'I am FOO'; // compiler error
var x : any = 3; // x can be anything
x = "45";
var foo : any = {};
foo.description = 'I am FOO'; // compiler is happy
var x; // x is any forever
x = '45'; // x is still any
function process(x): { // x is any
return x+x*3; // return type is any
}
process(42); // this does not change the type of x
var x: any;
if (typeof x === 'string') {
console.log(x.subtr(1)); // Error
}
// x is still any here
x.unknown(); // OK
class Animal { name:string }
class Cat extends Animal { meow() { } }
var pet: Animal = new Cat();
if (pet instanceof Cat) {
pet.meow(); // OK
} else {
pet.meow(); // Error
}
interface Animal {name: string; }
interface Cat extends Animal { meow(); }
function isCat(a: Animal): a is Cat {
return a.name === 'kitty';
}
var x: Animal;
if(isCat(x)) {
x.meow(); // OK, x is Cat in this block
}
declare var angular : any;17K lines of ambient declarations
eval, parseInt, encodeURI
Math, Date, RegExp
Full DOM declarations
And many more...
1458 contributors
15K commits
Thousands of definition files
node.d.ts