Jump to content

TypeScript

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by NaibStilgar (talk | contribs) at 12:54, 3 October 2012. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

TypeScript
Designed byMicrosoft
DeveloperMicrosoft
First appeared2012
LicenseApache License
Filename extensions.ts
Websitehttp://www.typescriptlang.org/
Influenced by
JavaScript

TypeScript is an open source programming language developed by Microsoft. It is a superset of JavaScript, and essentially adds optional static typing and class-based object oriented programming to the language. Anders Hejlsberg, lead architect of C#, has worked on development of TypeScript.[1][2][3][4]

TypeScript extends Javascript syntax, so any existing Javascript programs work with Typescript without any changes. Typescript is designed for development of large applications and when compiled it produces Javascript to ensure compatibility.[5]

Background

Language features

TypeScript is a language extension that adds features to JavaScript.

Type annotations

TypeScript provides static typing through type annotations to enable type checking at compile-time. This is optional and can be ignored to use the regular dynamic typing of Javascript.

function Add(left: number, right: number): number {
	return left + right;
}

The annotations for the primitive types are number, bool and string. Weakly, or dynamically, typed structures are of type Any.

Type annotations can be exported to a separate declarations file to make type information available for TypeScript scripts using types already compiled into JavaScript. Annotations can be declared for an existing JavaScript library, so has been done for Node.js and JQuery.

The TypeScript compiler makes use of type inference to infer types when types are not given. If no type can be inferred because of lack of declarations then it will default to the dynamic Any type.

ECMAScript 6 support

TypeScript adds support for features proposed for the upcoming ECMAScript 6 standard.

These are the constructs

  • Classes (with inheritance)
  • Modules
  • Arrow functions

Although the standard is not ready, Microsoft has said that it aims to align TypeScript's features with the proposed standard.

Classes

TypeScript supports ECMAScript 6 classes with type annotations as a supported option.

class Person {
    private name: string;
    private age: number;

    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
    
    toString(): string {
        return this.name + " (" + this.age + ")";
    }
}

Generics

The language specification states that a future version will support generic programming based on type erasure.

Compatibility with JavaScript

TypeScript is a superset of JavaScript. By default the compiler targets ECMA Script 3 (ES3) but ES5 is also supported as an option. A TypeScript application can consume existing Javascript scripts. Compiled TypeScript scripts can be consumed from JavaScript.

Existing frameworks such as JQuery and Node.js are fully supported. Type declarations for these libraries are provided with the source code.

Supported web browsers and platforms

Any web browser on any platform can run TypeScript as it is just compiled into standard JavaScript. A script can either be precompiled into JavaScript or compiled on the fly by including the JavaScript compiler for TypeScript.

Development tools

Compiler

The TypeScript compiler, or tsc, is written in TypeScript that can be compiled into regular JavaScript that can be executed in any JavaScript engine in any host, such as a browser. The compiler package comes bundled with a script host that can execute the compiler. It is also available as a Node.js package that is using Node.js as a host.

The current version of the compiler supports ECMAScript 3 by default. An option is allows to target ECMAScript 5 to make use of language features exclusive to that version. The language features implemented that will be part of the ECMAScript 6 standard are available in both modes.

IDE and editor support

Microsoft provides a plug-in for Visual Studio 2012 as well as basic text editor support for Sublime Text, EMACS and Vim [6]

The TypeScript website has an interactive playground that provides a rich coding experience.[7]

Open Source

TypeScript is open source and the source code is available from Codeplex under the Apache 2 License. The project is maintained by Microsoft but anyone can contribute by sending feedback, suggestions and bugfixes via the Codeplex project page.[8]

See also

References