Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the significant differences between TypeScript and Javascript? [closed]

What are the differences between Typescript and Javascript? What are the language design goals of each and how do these design goals differ?

How compatible are libraries and frameworks intended to be used with Javascript, for example jquery, also compatible with Typescript?

Some examples would be helpful.

like image 344
Bala Avatar asked Apr 20 '13 23:04

Bala


People also ask

What advantages does TypeScript have over JavaScript?

TypeScript is a superset of JavaScript, a statically aggregated language to compose straight forward JavaScript code. TypeScript gives discretionary static composing, classes, and interface, and is thought to have better code organizing and object-arranged programming procedures.

What is the benefit of using TypeScript compared to plain JavaScript?

A superset of JavaScript, TypeScript offers all of the features of JavaScript plus some additional perks. TypeScript intrinsically encourages us to code cleanly, making the code more scalable. However, projects can contain as much plain JavaScript as we like, so using TypeScript is not an all-or-nothing proposition.

How is TypeScript better than JavaScript?

TypeScript vs JavaScript: Highlights JavaScript is better suited for small-scale applications, while TypeScript is better for larger applications. TypeScript supports static typing but JavaScript does not. TypeScript supports interfaces but JavaScript does not. TypeScript features prototyping but JavaScript does not.

What are the differences between Java and TypeScript languages?

Java has methods, while TypeScript has functions. The two concepts are identical. It's only the syntax that is different. JavaScript and TypeScript are much more flexible in terms of syntax than Java, so with TypeScript, you may see methods that don't have a return type or don't have typed method parameters.


1 Answers

I've used typescript for a few small hobby projects and I'm convinced that it is the perfect Javascript replacement. My favorite features were:

  • Declaration files. With declaration files you can add type informations to your javascript libraries. This structural information will get you fantastic intellisense support in VisualStudio.

  • "Standard" OOP. If you come from a C# or Java background you probably won't even need a typescript tutorial. It just works. You have classes, interfaces, access modifiers, extension mechanisms, ...

  • Built-in support for modules. Typescript has a slightly confusing module system. You can split your code into several .ts files and just append them but you can also create different modules.

And finally: The syntax. Sometimes it's the small things that have the biggest impact. To me the syntax of typescript feels just perfect. Let me give you a few examples:

Type annotations with ":" and type inference

var a = 5; //infers that a has the type number
var canvas : HTMLCanvasElement = document.getElementById("canvas"); 
// automatically casts to the canvas type. Intellisense will now suggest all the canvas specific methods

Arrays that work as lists, stacks, ...

var array= []; //dynamic type
array.push(1);
array[1]=2;
array.pop();

var array2 : number[] = []; //typed array
array[0]=2;
array[1]="hello"  //compile time error. You've got to love the type system. Finally you can trust your collections

And functions with the arrow syntax as lambdas:

var array=[];
array.push(1);
//...
array.forEach((num)->{alert(num);});
//for single statement functions you can write
array.forEach((num)->alert(num));

Now typed arrays and lambdas combined:

var array: number[]=[];
array.push(1);
//...

//let's assume you want to work with the data in the array. You've got to filter it and process it. Lambdas will come in handy, as well as the type inference
array.filter((num)->num>3).map((num)->num*2).forEach((num)->alert(num));

// the first lambda with the comparison is fully type safe. The compiler knows the type of the array. Therefore it can infer the type of the parameter num and will check if num can be compared to a number

I really enjoyed using typescript. It will significantly increase your productivity. And there's even more to come: http://typescript.codeplex.com/wikipage?title=Roadmap https://github.com/Microsoft/TypeScript/wiki/Roadmap

The 0.9 release will feature generics and for the 1.x versions they plan to implement async/await calls.

like image 98
lhk Avatar answered Nov 16 '22 03:11

lhk