JavaScript or Typescript or both 🤷‍♀️?

Femi Adisa
3 min readMay 27, 2021

--

source: hackr.io

As developers, we sometimes get stuck deciding which frameworks/language to use, especially if we know more than one that suits our needs or want to learn. Oftentimes, learning more about what makes them unique points you in the right direction.

This blog covers what JavaScript and Typescript are, their uses, differences, and interoperability.

What is JavaScript?

According to MDN,

“JavaScript is a scripting or programming language that allows you to implement complex features on web pages”. — MDN

In my own words, JS is a programming language that gives behavior to your webpage, that is, it provides the ability for your webpage to interact with and react to changes made by the user. Simply put, it’s the main difference between a static site and a dynamic site.

What is Typescript

According to the official documentation,

“TypeScript is an open-source language which builds on JavaScript… by adding static type definitions.” — Typescript docs

Well said, but what’s static type definitions? Simply put, static typing is the art of declaring a variable before using it so that you can’t use variables you haven’t explicitly declared. That way, some developer errors (e.g. when you make a spelling mistake) will throw a syntax error instead of a semantics error (i.e it’ll throw an error during compile time instead of during run time) learn more here

How are they used/what are they used for?

Simply put, JavaScript is used for making webpages react to user interaction. Typescript gets compiled into JS to do the same thing JS does.

Are they interchangeable?

Since Typescript gets compiled to JS, it would be safe to say they are interchangeable, which in this context means you can use Typescript instead of JS or vice versa. TS is compatible with Babel, swc, and Sucrase but you’ll still need the typescript dependency (and you might need to enable isolatedModules).

Which should I use?

You probably guessed I won’t give a direct answer here 😄, it’s simple though. When deciding between JS and Typescript, you should take into consideration the size of the project, the current status (if its an ongoing project or just needs maintenance or you’re just starting out), the company culture/your personal preference, and your team’s opinions (especially if you’re thinking of switching from JS to TS), amidst other factors. Some people prefer plain old JS while some love the type-checking and early on error-catching that Typescript provides.

Getting started with typescript

Install Typescript in Node via

npm install typescript --save-dev
//or
yarn add typescript --dev

You can also install it as an extension on VSCode

Learn more about installing and using TS

Can I use both?

Yes! According to the official typescript documentation,

“Adopting TypeScript is not a binary choice, you can start by annotating existing JavaScript with JSDoc, then switch a few files to be checked by TypeScript and over time prepare your codebase to convert completely.” — Typescript documentation

While I don’t expect every reader to completely understand what the above quote means, you should know that since TS results in JS, they can be used together, with some modifications. I found this well detailed stack overflow post about using both.

--

--