- 4 years ago
- Afaq Arif
- 3,184 Views
-
2
In this chapter, we will learn about the components and features of TypeScript. We will also discuss why we use TypeScript.
JavaScript was presented as a language for the client-side. The advancement of Node.js has labeled JavaScript as an arising server-side technology too. However, as JavaScript code develops, it will become more complex, making it hard to keep up and regenerate the code. Besides, JavaScript can not become successful at the venture level as full-fledged server-side technology due to its inability to accept the characteristics of Object Orientation, strong type checking, and compile-time error checks. TypeScript came to fulfill this gap.
What is TypeScript?
By definition, “TypeScript is JavaScript for application-scale development.”
A TypeScript is an object-oriented, assembled, and strongly typed language. Anders Hejlsberg (designer of C#) at Microsoft designed it. It is both a language and a set of tools. It is a typed superset of JavaScript assembled to JavaScript. In simple, TypeScript is JavaScript with some extra features.
Features of TypeScript
TypeScript is just JavaScript. It begins with JavaScript and ends with JavaScript. You can use Typescript only if you know JavaScript as it accepts the basic building blocks of your program from JavaScript. You can also convert all TypeScript code into its JavaScript equivalent for execution purposes.
TypeScript supports other JS libraries. You can dominate Compiled TypeScript from any JavaScript code. You can regenerate all of the living JavaScript frameworks, tools, and libraries from TypeScript-generated JavaScript.
JavaScript is TypeScript. This means that you can call any valid .js file to .ts and can assemble with other TypeScript files.
TypeScript is portable. It is easy to move TypeScript across browsers, devices, and operating systems and can run on any platform that JavaScript runs on. In contrary to its equivalents, it does not need a devoted VM or a specific runtime environment to run.
TypeScript and ECMAScript
The ECMAScript specification is a standardized specification of a scripting language. We have six editions of ECMA-262 published. The codenamed for version 6 of the standard is “Harmony”. TypeScript is lined up with the ECMAScript6 specification.
TypeScript gets its basic language characteristics from the ECMAScript5 specification, i.e., the official specification for JavaScript. The language features of TypeScript like Modules and class-based orientation are in accordance with the EcmaScript6 specification. Furthermore, TypeScript will accept features like generics and type annotations which aren’t a part of the EcmaScript6 specification.
Why Use TypeScript?
TypeScript is an extended JavaScript. It is at a higher level as compared to its counterparts like CoffeeScript and Dart programming languages. In comparison, languages like Dart, CoffeeScript are new languages in themselves and need a language-specific running environment.
The benefits of TypeScript include −
- Compilation − JavaScript is an interpreted language so in order to check its validity, you need to run it. So if there is an error, you need to write all the codes just to find no output. You need to spend hours trying to find errors in the code. The TypeScript transpiler provides the error-checking feature. If TypeScript finds some kind of syntax errors, it will assemble the code and run compilation errors. It will help you to highlight errors before the script is run.
- Strong Static Typing − JavaScript is not strongly typed. TypeScript comes with an optional static typing and types inference system through the TLS (TypeScript Language Service). The type of a variable showing no type might be concluded by the TLS based on its worth.
- TypeScript supports type definitions for existing JavaScript libraries. TypeScript Definition file (with .d.ts extension) gives definition for external JavaScript libraries. Therefore, TypeScript code will have these libraries.
- TypeScript supports Object Oriented Programming ideas like classes, interfaces, inheritance, etc.
Components of TypeScript
TypeScript has the following three fundamentals components.
- Language − It consists of syntax, keywords, and type annotations.
- The TypeScript Compiler − The TypeScript compiler (tsc) changes the instructions written in TypeScript to its JavaScript equivalent.
- The TypeScript Language Service − The “Language Service” reveals an extra layer around the core compiler pipeline that are editor-like applications. The language service assists the common set of typical editor operations like statement completion, signature help, code formatting and outlining, colorization, etc.
Declaration Files
When you assemble a TypeScript script, you will get an option to generate a declaration file (with the extension .d.ts). This declaration file works as an to the components in the assembled JavaScript. The declaration file idea is comparable to the concept of header files found in C/C++. The declaration files (files with .d.ts extension) give IntelliSense for types, function calls, and variable support for JavaScript libraries like jQuery, MooTools, etc.
- 4 years ago
- Afaq Arif
- 3,184 Views
-
2