header ad

What is TypeScript?

What is TypeScript?

TypeScript is a JavaScript superset that focuses on optional static typing, classes, and interfaces. One of the major advantages is that it allows IDEs to provide a more comprehensive environment for detecting typical errors as you type code.

JavaScript was created by Netscape in 1995 as an object-based scripting language. It has a large development community and an open standard (ECMAScript). Microsoft created TypeScript in 2012 as an object-oriented programming language. It is a superset of JavaScript that may be used as a language as well as a set of tools. It has static typing as an option, which can assist catch errors as they occur, making it perfect for large group collaborations.

header ad

TypeScript makes use of a transcompiler, also known as a transpiler, which is a source-to-source compiler that converts one language into another. The TypeScript transpiler is a tool that transforms TypeScript code into JavaScript code.

What are the benefits of using TypeScript?

TypeScript solves the most significant drawback in JavaScript: a fault in JavaScript can only be recognized during runtime, resulting in applications that are released with defects. This will have a negative impact on any business because it will degrade the user experience. TypeScript solves this problem by checking for errors throughout the compilation process.

When using JavaScript, the following script will not generate any errors; but, when using TypeScript, the compiler will warn you that nonExistentProperty does not exist on object obj.

const obj = {
	name: "john",
};

console.log(obj.nonExistentProperty);
TypeScript boosts your productivity while also preventing bugs.

Types boost efficiency by assisting you in avoiding numerous errors. By using types, you can catch errors at compile time rather than waiting for them to appear at runtime. The following function adds two numbers a and b:

function add(a, b) {
   return a + b;
}

You might receive an unexpected result if you get the values from HTML input elements and pass them into the function:

header ad
let result = add(inputA.value, inputB.value);
console.log(result); // result of concatenating strings

For example, If users input 20 and 30, the add() function would return 2030 rather than 50.

The reason for this is that inputA.value and inputB.value are strings rather than numbers. When you use the + operator to join two strings together, it creates a single string.

When you use TypeScript to declare the type of the parameters explicitly, such as this:

function add(a: number, b: number) {
   return a + b;
}

We introduced number types as parameters in this function. Only numbers, not other values, are accepted by the function add().

When you use the following syntax to call the function:

let result = add(inputA.value, inputB.value);

If you try to compile TypeScript code into JavaScript, you’ll get an error message. As a result, you’ll be able to avoid the issue at runtime.

Manuals

  • TypeScriptLang Reference is the main manual with examples and other information. It’s great to get in-depth information about individual language functions, methods etc.

One can find it at https://www.typescriptlang.org/docs/

In This Module

Leave a Comment

Your email address will not be published.