DEV Community

Cover image for TS Part 1: What is TypeScript?
Samuel Littell
Samuel Littell

Posted on • Updated on

TS Part 1: What is TypeScript?

Typescript, put simply, is a superset of JavaScript that is object oriented and is considered "strongly typed". A strongly typed programming language is one in which restrictions are firmly enforced when mixing different data types and values. These restrictions can ensure a consistency of results, but at the sacrifice of flexibility. Whether or not that sacrifice is worth it is up to the developer or depends on the type of application being developed.

TypeScript was created in 2010 when JavaScript was still perceived as a programming language that wasn't really capable of handling large scale applications. Microsoft acknowledged this perception and chose to do something about it, thus creating what we now know as TypeScript. In 2012, Anders Hejlsberg, a core Microsoft developer of TypeScript, said "JavaScript was never really designed to be a programming language for big applications" and that JavaScript "lacked some of the structuring mechanisms that you need in large applications like classes and modules and perhaps interfaces." So Microsoft built TypeScript on top of JavaScript which allowed them to create the ideal development platform they desired, while still benefitting from the broader JavaScript ecosystem. Today most of Microsoft's former objections about JavaScript have actually been solved, but even with those improvements, TypeScript is still very valuable.

Anders Hejlsberg (co-creator of TypeScript)

Anders Hejlsberg

In dynamically typed languages like JavaScript, you don't necessarily think about the types of data in your code, at least not as much as you would in a statically typed languages. Variables, for instance, are containers for values, but when you use variables in your code, you may not necessarily be considering their types. In a larger apps you can have hundreds or thousands of variables in your code, and if you're not careful, you can introduce bugs by mistakenly referencing a variable with the wrong type. TypeScript allows the developer to define what type a variable will be when that variable is declared.

// JavaScript
let city = 'New Orleans';
let zip = 70130;
let visited = true;

// TypeScript
let city: string = 'New Orleans';
let zip: number = 70130;
let visited: boolean = true;
Enter fullscreen mode Exit fullscreen mode

So in the example above we see the city, zip, and visited variables declared in JavaScript, and then updated with Typescript. Now city must always contain a value with a type of string, zip with a type of number, and visited with a type of boolean.

TypeScript can also be thought of as a type of linter. It can validate your code similarly to Prettier or ESLint, and makes troubleshooting problematic code a bit easier. TypeScript can autocomplete your code as it's written, warn if a function is used incorrectly, alerts you if an object is missing a required property, or even something simple, like letting you know if a return value is missing at the end of a function.

Finally, TypeScript can also be thought of as a compiler, which means it can take JavaScript and TypeSript code and transform them to support different features for older JavaScript engines. So TypeScript is actually a bit more than simply just adding types to JavaScript!

Resources:

Top comments (0)