What is TypeScript?

Photo of author

By Vijay Singh Khatri

TypeScript is an object-oriented programming language developed by Anders Hejlsberg at Microsoft in 2012. It is a strongly-typed compiled language and also called a superset of JavaScript. You can use TypeScript as both a language and a set of tools. Further, you can use it for developing large applications because it provides an optional static typing feature which is helpful to detect errors easily.

In addition, you can use TypeScript in developing JavaScript applications for both client-side and server-side execution. First, you have to write TypeScript code, and then by using a TypeScript compiler, you can compile the TypeScript code into JavaScript code. All the TypeScript files have a .ts extension. It is also a portable language. You can run programs written in TypeScript on any browser, device, and operating system.

In this article, we are going to discuss features of TypeScript, its need in programming, different types of this language, some drawbacks, its future, and many other valuable topics related to this language. Let’s start the discussion.

Why is TypeScript Needed?

In the last few years, the popularity of TypeScript has increased rapidly. It is a type-checked language, so it helps you in understanding where to put what type of data in a program and also helps in removing the bugs during the development process. It also simplifies the code and makes it easy to read and understand.

Further, it is an object-oriented programming language, so you can use all the object-oriented concepts like class, interface, inheritance, and many more in your applications. All these features provide readability to your code and make it easy to understand. You can also validate connections between every part of your project quickly because it is a type-checked language and provides instant feedback during development.

Features of TypeScript

TypeScript is a strongly-typed, object-oriented, compiled language. When TypeScript was developed, its primary goal was to make the development of JavaScript applications efficient and to detect mistakes early. Other features that make this language unique are:

1. Object-Oriented Language

You can find all the features of an object-oriented programming language in TypeScript, such as classes, objects, inheritance, modules, etc. Both the client-side and server-side development of an application can be quickly done by using these features of TypeScript.

2. Portable Language

Another feature of TypeScript is its portability. You can run TypeScript on several platforms like browsers, operating systems, and other devices. Any environment that supports JavaScript applications also supports TypeScript applications. You don’t need the installation of any specific runtime environment for TypeScript applications.

3. Static Type-Checking

TypeScript provides a static type-checking feature. This feature allows you to check the program at compile-time, which further helps you to identify errors while typing the code. You don’t have to run the application every time because all the mistakes of code are found at compile time. You can add types to variables, functions, properties, etc., and also, types can provide some significant advantages to more extensive and complex applications.

4. Supports JavaScript Libraries

All the JavaScript elements like libraries, frameworks, and tools are supported by TypeScript without any issue. This feature allows you to use existing JavaScript code with TypeScript without making any changes. This makes the development process easy and efficient.

5. JavaScript is TypeScript

Both TypeScript and JavaScript are similar languages in terms of syntax and semantics. This further allows the JavaScript developers to learn TypeScript quickly. The similarity of these languages also will enable you to convert the code written in JavaScript with .js extension into TypeScript with .ts extension with the help of the TypeScript compiler.

Why Should You Be Using TypeScript?

TypeScript adds several features to JavaScript, such as strict typing, interfaces, and many more. Other things that make this language a popular choice among developers to create large and complex applications are:

1. Reduces Bugs

Bugs can break the biggest system. TypeScript is a reliable and strongly-typed language. These characteristics of TypeScript help a developer to write type-specific code and reduce bugs. Furthermore, TypeScript code is more reliable and easier to refactor. Mistakes in code can be easily found because of its compile-type feature. You don’t need to run the program again and again to detect errors.

2. Documents Itself

The self-explanatory code of TypeScript allows you to write type definitions in the project or library files directly. You can store valuable information in the code, which further helps other developers to understand it in the future. You can also understand the complexity of code without opening any separate file.

3. Great IDE Support

TypeScript provides you with an excellent Integrated Development Environment. Code auto-completion is much faster in TypeScript. If you are finding something that does not exist in your code, then the IDE of TypeScript tells you automatically about it without wasting any time.

4. Community Support

There is tremendous community support provided to TypeScript. Most popular tech companies like Google, Microsoft, Mozilla, and Shopify are using TypeScript in their applications. It is so popular that most developers use TypeScript over JavaScript. DefinitelyTyped and TypeStrong are the famous open-source contributors who are regularly developing tooling features of TypeScript.

5. Improves Project Quality

Functionalities of TypeScript help a developer to write a program quickly and comfortably and also allow them to maintain it properly. Developers also don’t have to worry about the small details because of the types and interface features provided by this language. So they can concentrate on other topics like logic in the application. All these things lead to improving the quality of the project.

Components of TypeScript

In programming, a system is divided into components. TypeScript is divided into three main segments. These components are:

1. Language

The first component of TypeScript is Language. It is the most important part for the developers to write a new programming language. All the language elements like syntax, keywords, and type annotations are included in it.

2. The TypeScript Compiler

Second component of TypeScript is TypeScript Compiler (TSC). It converts the TypeScript program into JavaScript code. It is open-source, cross-platform, and written in TypeScript. You can not execute TypeScript code directly.

So you have to rewrite the TypeScript code in JavaScript. TSC helps you in this rewriting process and also helps you to do the parsing and type-checking of TypeScript code to JavaScript code. You can install the TypeScript compiler locally or globally and then can easily compile the TypeScript file.

3. The TypeScript Language Service

The third component of TypeScript is TypeScript Language Service. This component of TypeScript supports some common editor operations like code formatting and outlining, signature help, statement completion, etc. It also gives information to editors, which helps them to provide some assistant features like automated refactoring and IntelliSense.

Different Types of TypeScript

Every programming language is based on some keywords and data types. The type system of a language shows the different types of values supported by it. You can also check the validity of the given values before using them in the program with the help of the type system. Bugs can also be automatically reduced by using this feature in the starting phase of development. Data types are part of this type of system in TypeScript. Here is the list of some data types used in TypeScript.

1. Numbers

TypeScript has only one numeric type, Number. It is a built-in data type in TypeScript. Float and integer data types are not supported by TypeScript. All the numbers in TypeScript are stored as floating-point values. You can represent both integers and fractions with the help of number data type. Binary, Octal, Decimal, and Hexadecimal literals are also supported by TypeScript. Example:

let a: number = 123;

2. String

The string is another built-in data type in TypeScript. This data type is used to store textual data. It is also called an important data type in TypeScript. You can use both single quotes (‘) and double quotes (“) to represent string data. Example:

let b: string = ‘John’;

3. Boolean

Another built-in data type in TypeScript is Boolean. It represents logical values, true and false. This data type is mostly used in conditional statements. It is also the most basic data type in TypeScript. Example:

let c: boolean = true;

4. Void

This data type is also a built-in type and does not return any type of value. It is used in cases where no data type is available. This data type is used in those functions where there is nothing to return. We can not use it on variables.

5. Null

This data type represents an intentional absence of an object value which means that we want to give value to the variable but not now. So we have to give null to that variable as a type. Example:

let d: null = null;

6. Any

It is an important data type used in TypeScript. When we don’t know what type of data is being used, then we can give it the ‘any’ data type. Example:

let e: any = 123;

7. Enum

Enum is a user-defined data type. This data type used in TypeScript is like an array. Enum defines a set of named constants. You can give the index number to these constants. By default, enums start indexing at 0, but you can change this by manually setting the value of its members.

8. Array

It is another user-defined data type in TypeScript. The array is a collection of elements of the same data type. There are two ways to declare arrays in TypeScript. The first way is to use the type of the elements followed by [] to denote an array of that element type. The second way uses a generic array type.

9. Tuple

This data type used in TypeScript is used to declare two sets of values of different data types. For example, you can represent a value as a pair of a string and a number in this data type.

TypeScript vs. JavaScript

As discussed above, TypeScript is a strongly-typed superset of JavaScript that compiles into plain JavaScript. The features that make TypeScript different from JavaScript are:

TypeScript JavaScript
TypeScript was developed by Microsoft in 2012.JavaScript was first developed In 1995 by Netscape.
TypeScript always provides support for static typing.Static is not an option when you are working with JavaScript.
The source file of TypeScript has a “.ts” extension.“.js” is the source file extension of your JavaScript code.
Various object-oriented concepts are supported by TypeScript, such as inheritance, interfaces, classes, etc.Object-oriented concepts are not supported by JavaScript. It is merely a scripting language.
TypeScript provides support for modules.When using JavaScript you cannot take the benefit of adding modules to your code.
Generics can be used in Typescript.There is no support for generics in JavaScript.
TypeScript is known to be a compiling language.It is an interpreted language.
It comes with a user-interface feature.JavaScript doesn’t come with any interface features to make it easier for you to see the outcome of your code.
TypeScript can work with prototyping.Moreover, you are not getting any prototyping features with JavaScript as well.
We need to compile Typescript code to make it work.JavaScript code can run without compiling the code.
It supports optional parameters.No optional parameters are supported in JavaScript.
You cannot run code that is written in Transcript directly on a web browser.A developer can run JavaScript code directly on the web browser.
TypeScript is useful when we are working on a large-scale project.JavaScript is useful for small-size projects.
It supports both static and dynamic types.Dynamic typing is available but you cannot do static typing.
You can specify type annotations in TypeScript.There is no way a developer can specify the type annotations.
For many developers, TypeScript is considered to be a heavy-weight language.JavaScript is considered to be a simple programming language.
Lastly, we can use TypeScript only on the client-side.Client-side and server-side can both take the benefits of using JavaScript.

Drawbacks of Using TypeScript

As we discussed earlier, there are many advantages of using TypeScript. It provides optional static typing, classes, and interface features. But there are also some drawbacks to this language. Let’s discuss some of them.

1. Complicating Typing System

There is no doubt that the typing system of TypeScript is a great tool that provides many facilities in writing code, but sometimes it appears to be a bit complicated. A long effort is required to manage the code properly, and it is also not flexible.

2. Excessive Code

Another drawback of TypeScript is that in TypeScript, you have to write more code, and it further slows down the development process. Additional annotations also make TypeScript files larger than plain JavaScript files.

3. Required Compilation

TypeScript files require compilation. First, you have to transform TypeScript files into JavaScript files by using the TypeScript compiler. After that, you can run the application in the browser.

4. Security Issues

All the type checks in TypeScript are performed only during compilation. Some bugs may arrive during the execution of the program because TypeScript can not fix some fundamental issues of JavaScript, and the compiler also can not find them.

5. Third-Party Library Support

Another drawback of TypeScript is that not all third-party libraries are available in it. If a particular library is not available in TypeScript and the development team wants to use it, then they have to write and maintain their library. This may increase the development time.

How to Install TypeScript?

Now it’s time to discuss the installation process of TypeScript. TypeScript is an open-source technology so that you can run it on any browser, device, or operating system. The tools required to write and test a TypeScript program are:

  1. Text editor like Notepad, Notepad++
  2. TypeScript Compiler

There are three ways to install TypeScript on your system. Let’s discuss them.

Install TypeScript using Node.js Package Manager (NPM)

Node.js is an open-source, cross-platform runtime environment. You can install the TypeScript package on your local machine with the help of NPM. If you are using the JavaScript framework for your TypeScript application, then you have to download Node.js and install it for the proper execution of the program. You can download NPM from the official site of Node. If you are downloading it for Windows, then after downloading, follow the given steps to install it.

  1. Download and run the .msi installer for Node.
  2. To check that Node has been installed successfully, open the command prompt and type node -v in it. If the installation is done properly, then this will show you the version of Node which you are installing.
  3. Now, you can install TypeScript by writing the following code in the command prompt.

npm install -g typescript

  1. If you want to check TypeScript is being installed properly, then type tsc -v in the command prompt.

Install TypeScript as NuGet Package

If you want to install TypeScript for your .NET or .NET Core projects, then you can install it as a NuGet package. To install it, follow the below steps.

  1. Right-click on a project node and open NuGet package manager.
  2. Now click on Manage NuGet Packages, and in the browser tab, search for typescript.
  3. All the packages related to TypeScript will be displayed there.
  4. From this list of all packages, select Microsoft.TypeScript.MSBuild and then click on the install button.
  5. Now, TypeScript will be installed in your .NET project.

Install TypeScript as a Plug-in in your IDE

TypeScript can be built on various development environments like Visual Studio, Eclipse, Brackets, etc. So you can install a specific TypeScript package or a plug-in for your IDE. Here we are discussing the Visual Studio Code development environment. Visual Studio Code is an open-source IDE from Visual Studio. You can run it on different platforms like macOS, Linux, and Windows. The steps to install Visual Studio Code for Windows are:

  1. Download VScode from the official site of Visual Studio.
  2. After downloading, start the setup process by double-clicking on VSCodeSetup.exe.
  3. After the setup process, all your files will now display in VScode IDE.
  4. You can right-click on the file name and then select the Reveal in Explorer option to open the file in file explorer.
  5. You can also open the file in the command prompt by right-clicking on the file name and selecting the Open in Command Prompt option.

Creating the First Program in TypeScript

After discussing the installation process of TypeScript, let’s write our first program in this language. Follow the below-given steps to write, compile and run the program.

  1. Open the text editor and write the following code in it.

var hello: string = “Hello”;

var all: string = “All”;

console.log(hello + “to” + all);

Save the file as hello.ts.

  1. Now open the command prompt and compile this TypeScript code by writing the following command on it.

tsc hello.ts

The above command will compile the TypeScript file hello.ts and generate a JavaScript file with the same name but with the .js extension at the same location.

  1. Now run this JavaScript file by using the following command in the command line.

node hello.js

After running the above command, the following output will be displayed on the command line.

“Hello to All”

Future of TypeScript

We have discussed earlier all the features, benefits, limitations of TypeScript. Now the question arises, what is the future of this language? Let’s discuss in detail to find the answer to this question.

As we all know, the official slogan for TypeScript is “JavaScript that scales”. In large-scale applications which can not be handled by JavaScript, we use TypeScript. The main reason behind this is that TypeScript is a robust language and can handle errors efficiently. You will be informed at the right time about errors so that you can write code smoothly and can focus on the logical and designing part of the program. It further improves the code quality and development experience.

In addition, TypeScript does not have much competition and has a great community. It is also highly adopted by complex open-source projects. You can also run TypeScript programs in any web browser or operating system, which makes it a perfect choice among developers. You can build large, scalable enterprise web applications by using TypeScript. Efficient optimization and additional typing information features of this language also speed up the execution time of the application, which makes it the developer’s first preference.

After discussing various qualities of this language, we can say that the future of TypeScript is bright. Many big companies like Google, Facebook, Microsoft, and others are using TypeScript for their applications.

Conclusion

Now at the end of this article, we have all the important information about TypeScript. Nowadays, apart from all the big companies, many start-ups are also working on TypeScript. All the features of object-oriented programming are provided by TypeScript, which further helps in designing and implementing the applications smoothly. The easy-to-read and easy-to-maintain code of TypeScript separates it from other programming languages.

Moreover, you can easily change your JavaScript projects into TypeScript because it supports all the JavaScript libraries. Continuous improvements are being made in TypeScript’s type system, which makes it an up-to-date language. I hope this article helps you in understanding this language easily and concisely.

Leave a Comment