Understanding TypeScript and ShortComings Of

TypeScript is a superset of JavaScript that facilitates writing robust applications by giving you type-safety and features such as modules, classes, and interfaces. Any valid javascript code will run as expected in Typescript.

Typescript is highly recommended for building large and complex programs, as it reduces an entire class of bugs and refactoring mistakes.

Javascript Vs Typescript: Difference

Typescript gives static type check ability that means it can detect and throw errors at compile time instead of runtime. With the new type-check/safety features, it is easier to enhance code quality and reduce bugs in production.

When Typescript code compiles, you can eliminate a lot of bugs. It like a suite of unit tests that run the moment you write code, and they catch the bugs even before you even run the tests.

  1. Javascript allows accessing properties that are not present.
const obj = { num1: 10, num2: 15 };const multiply = obj.num1 * obj.num22 ;
Typescript Vs JavaScript

The above code snippet despites having error in obj properties(num22) won’t show any error in Native javascript while in TS it does.

2. Static Type Checking

In .ts file, variable assigned once can’t be re-initialized with another data type, thus it shows an error while in Native javascript, it works fine.

Typescript Vs JavaScript

Features of TypeScript

We have already seen the two use cases of a static type language that how it can help in detecting removing potential bugs.

  1. Static Typing

Detecting errors in code without running it is referred to as static checking.

Determining what’s an error and what’s not based on the kinds of values being operated on is known as static type checking.

function sum(a:number, b:number):number{
return a+b;
sum(3, 5);

2. Classes and Interfaces

Just like Classes, Interfaces can extend each other. This allows you to copy the members of one interface into another, which gives you more flexibility in how you separate your interfaces into reusable components.

interface Employee{
firstName : string,
lastName : string,
employeeID : number
const p1 : Employee = {
firstName : 'Bob',
lastName : 'Dale',
employeeID : 395

3. Modules

Modules in TS are used just like Javascript. It can contain both code and declarations.

4. Compiles to JavaScript

TypeScript →ES6 →Babel → ES5

Typescript compiles down to Native Javascript. So, all your javascript will be valid in TypeScript.

To see how to run a Typescript file, click this LINK

tsc index.ts
// This will generate a `index.js` file

Types in TypeScript

  1. undefined, number, string, boolean, object, function, symbol
  2. No int, double, or other non-native JS types.
  3. Classes are standard JS prototypal classes.

Additional Pointers

  • Typescript doesn’t ship with a runtime.
  • ES6 syntax is handled, but ES6 operations are not.

That’s all for today folks.

NEXT POST → How to Setup and Run Typescript in NodeJS?

Liked what you read? Let’s connect on Twitter

This post was originally published at — https://dipakkr.co/what-is-typescript/

SDE | Ex-FrontBench | Blogger. I write about Startup, Self-Improvement, JavaScript, React and Node.