What is TS?

Learn via video courses
Topics Covered

Overview

TypeScript is an object-oriented programming language that was designed by Microsoft in the year 2012. This language is considered the superset of JavaScript. TypeScript extends the functionality of JavaScript by adding data types, classes, and other object-oriented features with type-checking. JavaScript is well suited for small-scale applications but TypeScript is used for large-scale applications. TypeScript can be used to develop JavaScript applications for both server-side and client-side execution.

What is TypeScript

JavaScript programs are TypeScript programs if the respective JavaScript program doesn't have syntax errors. TypeScript programs consist of modules, functions, variables, comments, and expressions like a fully-fledged programming language. TypeScript code has been written first, then it will be converted to plain JavaScript code using the TypeScript compiler. This is to be done to run the TypeScript code in the browser window. The files of TypeScript are saved with the .ts extension rather than .js which is the extension for saving JavaScript files. Thetsc or the Babel compiler can be used to convert TypeScript to JavaScript.

typescript-and-javascript

Components of TypeScript

Internally, the TypeScript language has been divided into three main layers. Each layer is divided into different components. The following are the layers:

layers-of-typescript

  1. Language: It features the TypeScript language elements which consist of elements like syntax, keywords, and, type annotations.
  2. TypeScript compiler: It is responsible to convert the TypeScript program to its equivalent JavaScript code. The browser is not responsible for executing the TypeScript code directly. That's why the TypeScript code is changed in the code equivalent to the JavaScript code to perform execution in the Browser. For this action to be performed, TypeScript comes with a TypeScript compiler named tsc. TypeScript compiler can be installed on the local machine with the npm package. As soon as the installation completes, the TypeScript file can be compiled by executing the tsc command in the command line.

Example

The above statement compiles the helloworld.ts file into the helloworld.js file.

Compiler Configuration

The TypeScript compiler configuration will be given in the tsconfig.json file and will be as follows:

  1. TypeScript Language Services - The TypeScript language service provides the relevant information which helps the editors and other tools to give better features such as IntelliSense and automated refactoring. It supports some standard editor operations like statement completion, signature help, code formatting and outlining, etc.

The Gaps in JavaScript

JavaScript is considered a loosely typed programming language as it was originally developed by ECMA's Technical Committee 39 to work as a client-side programming language. When Node.Js came into effect, JavaScript is emerging as a server-side programming language also. As JavaScript is growing rapidly, it is getting more complex for developers to keep things tidy as they maintain and reuse the code. Also, the features like strong type checking, object orientation, and compile time error checks are considered the roadblocks for JavaScript to becoming a fully-fledged server-side programming language.

How TypeScript Fills in the Gaps in JavaScript?

TypeScript is a strongly typed programming language which a superset of JavaScript so it is the primary goal of TypeScript to fill the gaps in JavaScript so that it can enable application-scale development better. Typescript makes it easy to standardize the project, helps users catch logical errors on the fly, and overall gives more confidence to the users.

  • Compilation - As JavaScript is an interpreted language, hence it is difficult to find bugs and errors in the code. That's why TypeScript came into effect which helps in making error checking easier. TypeScript compiles the code and gives a compilation error if there arise syntax errors in the code. Therefore, errors can be handled and resolved at the compile time instead of runtime in TypeScript.
  • Static Typing and Type Inference - With TypeScript language services, it has an optional static typing and type inference system that can conclude undeclared variables. Hence, by adding type support to JavaScript, TypeScript can effectively remove type errors during compilation.
  • Emphasized IDE Support - TypeScript has great and enhanced IDE support which improves the development experience for TypeScript developers. It is easier to catch errors if any pop up while coding. Compilation errors can be identified with a red line shown in the IDE. Also, a code completion feature is there in which users can get inline help for any library which performs a specific task.

Why Use TypeScript?

What is TypeScript?

TypeScript is a strongly typed programming language that can detect errors in the code during the compile time. This programming language is backed by Microsoft, hence, it has a very active community of developers which helps TypeScript to evolve at a faster pace. Due to that reason, new versions are released regularly.

Some of the benefits of TypeScript are as follows:

  1. Support - TypeScript provides support for Object Oriented Programming concepts like Classes, Interfaces, Inheritance, etc.
  2. Compilation - TypeScript compiler provides the feature of error checking. If there is a syntax error in the code, then TypeScript will generate a compilation error so that the error could get highlighted before runtime.
  3. Strong Static Typing - TypeScript has the feature of strong static typing which comes through TLS(TypeScript language service). If a variable is declared with no type specified then TLS can conclude it based on its value.

Features of TypeScript

Several features of TypeScript define it differently from other programming languages. Some of them are as follows:

  1. Object Oriented Programming Language - TypeScript provides support for Object Oriented Programming concepts like Classes, Interfaces, Inheritance, etc. TypeScript code can be written for both client-side and server-side development.
  2. Supports Type Definitions - It supports type definitions for existing JavaScript libraries. TypeScript definition file that has the .d.ts extension defines external JavaScript libraries.
  3. Code Readability in TypeScript - Its code is written using classes and interfaces. They provide organization to the program and therefore it is easy to maintain and debug the code.
  4. TypeScript is Reliable - It has types that care for agility while re-structuring the code. It catches type-related errors at compile time rather than waiting for the runtime.
  5. Static Type Checking - TypeScript uses static typing which is done using type annotations. It helps type checking at compile time. Hence, errors can be found while typing the code without running the code every time.

features-of-typescript

TypeScript and EcmaScript

TypeScript can be used with Node.Js and its code when compiled to JavaScript can be used in any browser that supports EcmaScript 3 or above. It can help build and manage large-scale JavaScript projects.

EcmaScript is a standardized scripting language that was developed by ECMA International. This language is mostly used by web developers for client-side scripting. It has various features which aim at making large-scale software development easier.

Difference between TypeScript and EcmaScript

TypeScriptEcmaScript
TypeScript support all primitive data types.EcmaScript doesn't support all kinds of data types.
TypeScript has three scopes: Global scope, Class scope, and Local scope.EcmaScript has two scopes: Global and local scope.
Interfaces, Enums, Inference, Generics and type annotations are included in TypeScript.These features are not supported in EcmaScript.
In TypeScript, modules are of two types: Internal and external modules.In EcmaScript, modules can be used by importing and exporting.
The list of companies that are using TypeScript are Slack, Asana, Intuit, CircleCI, etc.The list of companies that are using EcmaScript are Slack, Asana, Intuit, eBay, StackShare, etc.

Features of EcmaScript

EcmaScript 2015 or ES6 is the 6th major edition of the EcmaScript language specification standard. ES6 comes with various significant features which brought changes to the JavaScript language.

All the browsers and JavaScript runtime environments don't EcmaScript standards. That's why, Developers use TypeScript as it supports many of the latest EcmaScript features like Modules, Classes, Spread operator, Lambda functions, etc. Also, it allows users to interpret EcmaScript features in older versions. This provides the capability to use upgraded features while having assurance for backward compatibility with older browsers and JavaScript runtimes. This allows developers to write code easily and efficiently.

Advantages of TypeScript

  1. TypeScript code can be run on any browser or JavaScript engine after being transpile to JavaScript.
  2. TypeScript programs can be run on existing JavaScript libraries and frameworks without any problem.
  3. TypeScript involves type checking that allows the user to validate the type of any variable at runtime.
  4. TypeScript is useful for large team collaborations.
  5. TypeScript compiler can identify the compilation errors in the code at the time of development with the help of static typing and offers a less painful development experience.
  6. TypeScript is easy to learn for developers who are currently coding in JavaScript as its syntax and semantics resemble JavaScript to an extent.
  7. TypeScript code is closer in syntax to some backend languages like Java and Scala. For this reason, this helps the backend developers to write the front-end code faster.

Disadvantages of TypeScript

  1. TypeScript code involves a long time for compilation due to code conversion in JavaScript.
  2. It does not support the concept of abstract classes.
  3. When using an outside library, there should be a definition document, at times, it's not generally accessible.

TypeScript Competitors

CoffeeScript and Dart are the most popular languages that compile JavaScript. TypeScript consistently stands out among its competitors. For example, CoffeeScript is a lightweight language that aims to simplify JavaScript but it lacks the optional static typing afforded by TypeScript. Dart is also a typed superset of JavaScript which is considered the full replacement of the language. However, the most challenging for these competitors is interoperability with JavaScript as these are new languages. For these languages, an extension to JavaScript requires more language-specific execution. On the other hand, TypeScript is clear in its superiority in extending to JavaScript.

Conclusion

  • We get to know about the Components of TypeScript which are language, TypeScript compiler, and TypeScript language services.
  • We get to know about the Gaps in JavaScript and How TypeScript helps to fill the Gaps in JavaScript.
  • We get to know about EcmaScript and its features.
  • We get to know about the Features, Advantages, and Disadvantages of TypeScript.
  • Hope this article has provided you with the relevant information regarding the TypeScript programming language.

See Also

To get more familiar with the TypeScript programming language, a developer should have an understanding of JavaScript programming language. So, To get a sound knowledge of JavaScript, refer to: