Master any programming language fast

The TypeScript Programming Language

Share
Like SyntaxCorect.com on Facebook
Be the first to rate this article

Bugs, developer productivity miserably sinking, the most advanced IDE tools forced to uselessness... Every seasoned JavaScript developer knows how JavaScript can hurt when projects get really big. But, wait, that's exactly why TypeScript is here!

Introduction

TypeScript is a computer programing language first appeared on the scene 12 years ago (on October 1, 2012). It is an interpreted, high-level, general-purpose, dynamically, weakly typed, object-oriented, imperative, functional and procedural programming language with automatic memory management directly derived from JavaScript of which it is a superset, being any JavaScript program also a valid TypeScript program.

TypeScript has been developed and is maintained by Microsoft. The language has been created by Anders Hejlsberg, also the creator of C#, Delphi and Turbo Pascal. The main purpose of the language is to provide extensions to JavaScript that better support the development of large applications for JavaScript execution environments, that means for web-applications executed in browsers, or server-side applications executed on Node.js. The language accomplishes its goals in two ways:

  1. it has been designed to be completely compatible with JavaScript and, to be more precise, to be a sort of preview of what JavaScript was going to become those days with the proposals from the ECMAScript 2015 standard at the time still in evolution;
  2. it adds optional static typing of variables. This is a peculiar feature of TypeScript not proposed in any ECMAScript standard version but that greatly increase programmers productivity. In fact, thanks to it, IDE tools can provide static language analysis to programmers during the development, which means that features like intelligent auto code completion, jump to source for classes and method members and the like can be implemented in the most optimal and effective way.

First glance at the code

Example: The classic "Hello world" program written in TypeScript:

Main features of TypeScript

Technical features

Type system

TypeScript, like JavaScript, has a dynamic, weak type system. Any variable can be assigned any type of value and the language makes automatic assumptions on how to make operations on different type of variables allowing for example the sum between strings and numbers. However TypeScritp also introduces what is called Type annotation, that means that programmers can optionally specify the type of the variables, enabling this way IDE tools to provide better intelligent auto code completion and so on. The TypeScript compiler generates errors if at compilation time it detects incompatible types being used, but it still produces the final JavaScript program so that it can be run anyway.

Basic types in TypeScript are very similar to those of JavaScript with some notable additions.

Table: TypeScript basic data types

Name Type Notes
boolean true/false  
number double-precision 64-bit floating point format (IEEE 754)  
string a sequence of Unicode UTF-16 code units  
Array   like in JavaScript but it's possible to annotate a type
Ex.: let list: number[] = [1, 2, 3];
Tuple   the same as the Array but useful to specify types
Ex.: let x: [string, number];
enum enumeration like in C# and other languages  
Any   useful to opt-out of type-checking at compilation time
Void   used as the return type of functions that do not return a value
undefined   this is the type (which has a corresponding unique value with the same name) automatically associated to variables that have never been assigned a value
null    
Never   this type represents the type of values that never occur

Object-oriented

TypeScript is object-oriented. It supports common object-oriented patterns like interfaces, classes and also supports generics and mixins. All the classes constructs are the same of the ECMAScript 2015 standard with the optional addition of type annotation.

Memory management

Memory management in TypeScript is automatic and is delegated to the underlying JavaScript execution environment.

TypeScript libraries/ecosystem

As a language transpiled to JavaScript, TypeScript can access and use any library available for JavaScript. Not only. TypeScript introduces the support of definition files which contain type information of existing JavaScript libraries. This enables other programs to use the values defined in the files as if they were statically typed TypeScript entities. Nowadays there are third-party TypeScript definition files for almost every popular JavaScript library and most of the times the same authors of a JavaScript library also includes in the sources the TypeScript definition files for the library. TypeScript definition files have become so common that they are even used by other statically typed languages who are also transpiled to JavaScript like Kotlin and others.

Performance

Being TypeScript sources transpiled to JavaScript, performance considerations are the same of those for JavaScript.

Other features

Free and open source

TypeScript is free and open source software licensed by Microsoft under the Apache License 2.0.

Best books for learning TypeScript

For beginners

TypeScript 2.x By Example: Build engaging applications with TypeScript, Angular, and NativeScript on the Azure platform

Leverage the power of Typescript 2.0 using real-world examples

Begin with the fundamentals of TypeScript and learn how to write better JavaScript code. Build three amazing applications throughout the book. Leverage the power of tools such as Angular 2 and... (continue on Amazon)

For experienced programmers

Pro TypeScript: Application-Scale JavaScript Development

Understanding the runtime and the TypeScript compiler

This fully revised and updated second edition of Steve Fenton’s popular book covers everything you need to discover this fascinating language and transform your experience of... (continue on Amazon)

Conclusion

Any seasoned JavaScript developer knows how JavaScript can hurt without the support of a static type system when large applications are developed. TypeScript solves the problem the most elegant and pratical way: it adds type annotation without breaking nothing at all from existing JavaScript, providing instead, in most cases, even a preview of what will be the next versions of JavaScript. It's no surprise that everyday some new big organization starts adopting it.