Learning TypeScript (part 1)

Photo by Tim Mossholder on Unsplash

Typescript is a language that has been quickly gaining traction in the programming world and something that I have been interested in learning for a while. Therefore, I thought the best way to show what I am learning is to blog about it. Therefore, this is part 1 of my journey into learning TypeScript where I will cover what TypeScript is and static typing.

What is TypeScript?

Typescript is a typed superset of JavaScript and was created by Microsoft in 2012. What does superset mean? JavaScript code is valid Typescript code, but Typescript comes with some additional features like a robust type system and object-oriented programming. So, a programmer could easily migrate JavaScript projects to TypeScript and progressively factor in types. After compiling the TypeScript code, all the Typescript stuff goes away and compiles to clean JavaScript code.

TypeScript was made with the vision to help JavaScript developers with large scale applications. If you have been using JavaScript, something you would notice how often errors occur. TypeScript’s type system helps avoid common errors such as passing the wrong parameters, that otherwise, would slow us down immensely. These errors will only be caught at runtime when your code is being executed in production which takes more time to debug. This type system not only reduces accidental errors like storing integer variables into string or vice-versa but also make our code more readable.

Static Typing

JavaScript is a dynamic scripting language (with object oriented programming for support) whereas TypeScript is an object oriented programming language that is a superset of JavaScript. TypeScript also has static typing whereas JavaScript does not. Static typing is when the compiler enforces values to use the same type.

Let foo = 5foo = “bananas”

A good and bad thing about JavaScript is that it is very flexible. Here, the type of ‘foo’ changes from a number to a string. In TypeScript, this is will cause an error because ‘foo’ is not allowed to change types. This makes our code more scalable and reliable, and we can check that our code runs properly before runtime. This will help mitigate the pitfalls of JavaScript that are only found at runtime. This feature also makes the code easier to refactor, since we can navigate to references of functions members.

TypeScript also has many other benefits. Typescript uses NPM and supports JS libraries and API documentation, such as jQuery, react.js, angular.js, and bootstrap. This makes TypeScript easier to learn assuming you somewhat know what you are doing in JavaScript. TypeScript is easier to maintain as it is considered generally easier to read and access.

Resources

https://www.educative.io/blog/typescript-tutorial#what

https://codeburst.io/understanding-typescript-basics-e003dbad2191

https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html

https://www.section.io/engineering-education/typescript-static-typing/

Flatiron School software engineering alum. Experienced in JavaScript, React.js, Ruby, and Ruby on Rails. https://www.linkedin.com/in/chandler-hanson/