Better client side programming with TypeScript
Background -
For .Net, C# or Visual Basic programmers, it is a common complaint that JavaScript (JS) is hard to work with, as there is no strong typing and class structures when dealing with large application. This is where TypeScript is worth considering.
“TypeScript is a typed superset of JavaScript that compiles to plain JavaScript”.TypeScript is open-source language from Microsoft’s C# team which is developed for building large-scale JavaScript applications. TypeScript is not a replacement of JavaScript, it does not add any new features in JavaScript. TypeScript provides developers with features like type safety, compile time type checking, object oriented constructs on top of JavaScript, basically lets developers think in object oriented terms when writing JavaScript.
TypeScript Fundamentals
TypScript Types-TypeScript provides some primitive types (shown below) as well as a dynamic type “any”. In TypeScript, we define a variable with a type by just appending the variable name with colon followed by the type name as shown in below example
var num: number = 50; //variable num is of type number
Below is the list of primitive types available in TypeScript
- Number: the “number” is a primitive number type in TypeScript. There is no different type for float or double in TypeScript
- Boolean: The “boolean” type represents true or false condition
- String: The “string” represent sequence of characters similar to C#
- Null: The “null” is a special type which assigns null value to a variable
- Undefined: The “undefined” is also a special type and can be assigned to any variable
- Array Type: TypeScript also allows developers to create array objects similar to that in .Net by just adding square brackets as shown in below example
This allows us to create complex object in TypeScript with primitive types. In TypeScript array is accessed with zero based index.
TypeScript also allows us to create complex variables as shown in below example
Classes -
In TypeScript a class can be defined with keyword “class” followed by class name. TypeScript classes can contain constructor, fields, properties and functions. TypeScript allows developers to define the scope of variable inside classes as “public” or “private”. It’s important to note that the “public/private” keyword are only available in TypeScript, once it’s converted to JavaScript there is no way to distinguish between the two and both can be called. TypeScript defines a constructor using keyword “constructor”.
TypeScript’s main benefits:
1.Class and Module Support
2.Static Type-checking
4.Clear Library API Definition
5.Build-in Support for JavaScript Packaging
6.Syntax Similarity to Our Backend Languages (Java, Scala)
Conclusion -There is so much more! We’ve barely scratched the surface but I hope this will get you started .If you want to play around with more examples online check out the Playground on TypeScript’s website.
For each of the features in TypeScript we have been relating to corresponding features in OOPs language such as C# and Java but, we need to keep in mind that TypeScript is not a OOPs language. In fact, TypeScript is just a tool on top of JavaScript which provides us with more robust code structure, type safety. TypeScript enhances the productivity of developers writing JavaScript code and it in itself does not provide any specific functionalities or features like libraries such as Jquery.
So,as you can see TypeScript can do the same basic OOP things that any other OOP language can. TypeScript definitely has a lot going on for it right now, I hope that you have found this post helpful, Thank you!
Good content!!
Great !!!! Awesome Article Ashwini
easy to understand post about what is Typescript.
Nice !!
Good. Does it has to use any specific IDE for this script?