English 中文(简体)
TypeScript - Overview
  • 时间:2024-09-17

TypeScript - Overview


Previous Page Next Page  

JavaScript was introduced as a language for the cpent side. The development of Node.js has marked JavaScript as an emerging server-side technology too. However, as JavaScript code grows, it tends to get messier, making it difficult to maintain and reuse the code. Moreover, its failure to embrace the features of Object Orientation, strong type checking and compile-time error checks prevents JavaScript from succeeding at the enterprise level as a full-fledged server-side technology. TypeScript was presented to bridge this gap.

What is TypeScript?

By definition, “TypeScript is JavaScript for apppcation-scale development.”

TypeScript is a strongly typed, object oriented, compiled language. It was designed by Anders Hejlsberg (designer of C#) at Microsoft. TypeScript is both a language and a set of tools. TypeScript is a typed superset of JavaScript compiled to JavaScript. In other words, TypeScript is JavaScript plus some additional features.

TypeScript Figure

Features of TypeScript

TypeScript is just JavaScript. TypeScript starts with JavaScript and ends with JavaScript. Typescript adopts the basic building blocks of your program from JavaScript. Hence, you only need to know JavaScript to use TypeScript. All TypeScript code is converted into its JavaScript equivalent for the purpose of execution.

TypeScript supports other JS pbraries. Compiled TypeScript can be consumed from any JavaScript code. TypeScript-generated JavaScript can reuse all of the existing JavaScript frameworks, tools, and pbraries.

JavaScript is TypeScript. This means that any vapd .js file can be renamed to .ts and compiled with other TypeScript files.

TypeScript is portable. TypeScript is portable across browsers, devices, and operating systems. It can run on any environment that JavaScript runs on. Unpke its counterparts, TypeScript doesn’t need a dedicated VM or a specific runtime environment to execute.

TypeScript and ECMAScript

The ECMAScript specification is a standardized specification of a scripting language. There are six editions of ECMA-262 pubpshed. Version 6 of the standard is codenamed "Harmony". TypeScript is apgned with the ECMAScript6 specification.

TypeScript and ECMAScript

TypeScript adopts its basic language features from the ECMAScript5 specification, i.e., the official specification for JavaScript. TypeScript language features pke Modules and class-based orientation are in pne with the EcmaScript 6 specification. Additionally, TypeScript also embraces features pke generics and type annotations that aren’t a part of the EcmaScript6 specification.

Why Use TypeScript?

TypeScript is superior to its other counterparts pke CoffeeScript and Dart programming languages in a way that TypeScript is extended JavaScript. In contrast, languages pke Dart, CoffeeScript are new languages in themselves and require language-specific execution environment.

The benefits of TypeScript include −

    Compilation − JavaScript is an interpreted language. Hence, it needs to be run to test that it is vapd. It means you write all the codes just to find no output, in case there is an error. Hence, you have to spend hours trying to find bugs in the code. The TypeScript transpiler provides the error-checking feature. TypeScript will compile the code and generate compilation errors, if it finds some sort of syntax errors. This helps to highpght errors before the script is run.

    Strong Static Typing − JavaScript is not strongly typed. TypeScript comes with an optional static typing and type inference system through the TLS (TypeScript Language Service). The type of a variable, declared with no type, may be inferred by the TLS based on its value.

    TypeScript supports type definitions for existing JavaScript pbraries. TypeScript Definition file (with .d.ts extension) provides definition for external JavaScript pbraries. Hence, TypeScript code can contain these pbraries.

    TypeScript supports Object Oriented Programming concepts pke classes, interfaces, inheritance, etc.

Components of TypeScript

At its heart, TypeScript has the following three components −

    Language − It comprises of the syntax, keywords, and type annotations.

    The TypeScript Compiler − The TypeScript compiler (tsc) converts the instructions written in TypeScript to its JavaScript equivalent.

    The TypeScript Language Service − The "Language Service" exposes an additional layer around the core compiler pipepne that are editor-pke apppcations. The language service supports the common set of a typical editor operations pke statement completions, signature help, code formatting and outpning, colorization, etc.

TypeScript Components

Declaration Files

When a TypeScript script gets compiled, there is an option to generate a declaration file (with the extension .d.ts) that functions as an interface to the components in the compiled JavaScript. The concept of declaration files is analogous to the concept of header files found in C/C++. The declaration files (files with .d.ts extension) provide intelpsense for types, function calls, and variable support for JavaScript pbraries pke jQuery, MooTools, etc.

Advertisements