What is TypeScript

SHARE

TypeScript

TypeScript is an open-source programming language developed by Microsoft. It is a strict syntactical superset of JavaScript that adds optional static typing to the language. It is designed for building large applications and transpiling to JavaScript. As it is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs.

TypeScript vs JavaScript

The main addition it brings to JavaScript is a type system (hence the name "TypeScript"). A type is a definition that specifies the purpose of data.

Traditionally, JavaScript is dynamically typed (like Python). This means a single variable can hold text, a number, or even an entire database entity, depending on the situation. What TS does is strictly define what a given variable can hold.

The main difference is that they are two separate programming languages, although it is based on JavaScript. In fact, TypeScript - as mentioned earlier - is a superset of JavaScript, meaning that all valid JavaScript code is also valid TypeScript code.

These are the advantages of TypeScript

TypeScript has the same benefits as JavaScript but also provides additional benefits that come from static typing and other concepts in TS.

Static typing

TS introduces optional static typing: once declared, a variable does not change the type and can only take certain values. The compiler warns developers of type-related errors. This results in less error-prone code and better performance during execution.

But static typing is not only meant to catch bugs. It also gives the code more structure, makes it self-documenting and readable, and speeds up debugging and refactoring. All in all, it increases productivity in a large team.

Quickly discover bugs

Researchers found that TypeScript detects 15% of common bugs in the compile phase. Significant enough to save developers time and allow them to focus on fixing errors.

Predictable

With TypeScript, everything stays as originally defined. If a variable is noted as a string, it will always remain a string and not change to a Boolean.

Other benefits include: 

The disadvantages

These are the main disadvantages.

Not worth it for small projects

If you're building something small and simple, TypeScript may not help much. The extra overhead of adding the TypeScript compiler is probably not worth it. Sometimes setting up and debugging TypeScript errors takes longer than fixing JavaScript errors.

Extra complexity

Browsers cannot execute TypeScript directly. So, if you plan to use it for front-end development, you will likely need to use webpack or a similar bundler to compile your assets. This extra installation step may be unnecessary if you can use vanilla JavaScript otherwise. 

Frequently Asked Questions
What is TypeScript and how is it different from JavaScript?

TypeScript is an open-source programming language that is a superset of JavaScript. It adds features such as static typing, classes, and interfaces to JavaScript, making it more robust and easier to scale. TypeScript code is transpiled to JavaScript code before it is run in a web browser or on a server.


What are the benefits of using TypeScript?

TypeScript offers several benefits, including improved developer productivity, better code organisation and maintainability, and reduced potential for runtime errors. With TypeScript, developers can catch errors at compile-time rather than at runtime, allowing for faster debugging and less downtime. TypeScript also provides better IDE support and code documentation.


How do I install and set up TypeScript?

You can install TypeScript using the Node Package Manager (NPM) by running the command "npm install -g typescript" in the terminal. Once installed, you can create a TypeScript file with the ".ts" extension, and compile it using the "tsc" command.


What are interfaces in TypeScript?

Interfaces in TypeScript define the shape of an object, specifying the properties and their types that must be implemented by an object that adheres to the interface. Interfaces help to enforce consistency across objects and can be used to create complex types.


Articles you might enjoy

Piqued your interest?

We'd love to tell you more.

Contact us