My background in programming languages has been strongly typed languages like Java and C thus when I first saw JavaScript I was little taken aback by the fact that you don’t have to declare the type of your variable before using it. I see the advantages, it does lend itself to easily start programming and I believe if you are doing some sort of algorithm coding problem it will save you a ton of declaration code and syntaxes you need to remember. JavaScript also makes complex code difficult to debug especially if its poorly documented.
In my search for a solution I stumbled upon TypeScript and this is an article about a beginner’s review of TypeScript and I have formatted it in a question answer way in order of how I was thinking about TypeScript.
- What is TypeScript?
TypeScript is a strongly typed programming language that builds on JavaScript , giving you better tooling at any scale. (https://www.TypeScriptlang.org)
- How to install TypeScript?
There are various resources on the internet to install TypeScript , I use the Atom IDE so these are the steps I followed:
- 1 Select Atom -> Preferences -> Install
- 2. Search for “TypeScript”, and the package atom-TypeScript is shown which has the highest number of installs (1.9 M when I was looking)
- 3. Search for atom-ide-ui and install it
- 4. You are all set
Other ways to install TypeScript can be found at – https://www.TypeScriptlang.org/download
- How to start writing some TypeScript code?
- Create a file in Atom with the .ts extension
- Paste below lines of code :
const obj = { width: 10, height: 15 };
const area = obj.width * obj.height;
console.log(area)
- In Atom click on packages and click build, this will create a .js file from the TypeScript file and you can run this file if you have the script package installed in Atom
- How is this better than Javascript?
Now lets see the power of TypeScript, in the above code replace the second line of code as below :
const area = obj.width * obj.heigth;
In regular JavaScript this will not show any error until you run the script when it will return result as NaN since it will consider obj.heigth as a new property of obj that has not been set
In TypeScript you will see a swiggly red line under the obj.heigth which will inform you that height is not a property that exists thus saving you a lot of pain when trying to debug complex programs
- Why would I not use TypeScript?
- It can be a little inconvenient to keep compiling your code to create JavaScript file
- Reuse of JavaScript libraries is additional work (check : https://www.techiediaries.com/use-external-javascript-libraries-typescript-projects/)
- Can be an overkill for small projects
Overall I read that TypeScript is the way to go when you are dealing with large projects with multiple developers, and I see the benefits of this having experienced the difficulties in debugging python code, so I will be starting to use TypeScript as and when it’s appropriate to write JavaScript code.
References:
- https://www.TypeScriptlang.org
- https://atom.io/packages/atom-TypeScript
- https://www.quora.com/If-TypeScript-is-a-superset-of-JavaScript-why-do-people-still-use-JavaScript
Blog created by Ragesh
