Pular para o conteúdo

Conheça Walt Disney World

TypeScript

TypeScript
Appeared in 2012
Designed by Microsoft
Developer Microsoft
Influenced by JavaScript, Java, C#
License Apache License
Usual filename extensions .ts
Website http://www.typescriptlang.org/

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]

Contents

Background

TypeScript originates from the need to develop application-scale JavaScript applications. The people behind the language at Microsoft have said that internal as well as external customers expressed problems structuring their code in JavaScript.

Many developers that ultimately rely on JavaScript usually write scripts in another language that compiles into JavaScript code, for example CoffeeScript and Script#. One noticeable disadvantage is that it might not be possible to use any specific language features of JavaScript from that other language if it does not support it.

Internally at Microsoft it lead to demand for custom tooling to ease the writing of components in JavaScript.

Language features

TypeScript is a language extension that adds features to JavaScript.

Syntactically, TypeScript is very similar to JScript .NET, another Microsoft implementation of the ECMA-262 language standard that added support for static typing, classical object orientation language features such as classes, inheritance, interfaces, and namespaces.

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.

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. In the process the compiler basically strips away all function and method bodies and preserves only the signatures of the types that are exported. The resulting declaration file can then be used to describe the exported virtual TypeScript types of a JavaScript library or module when a third-party developer consumes it from TypeScript.

The concept of declaration files is analogous to the concept of header file found in C/C++.

   module Arithmetics {
       add(left: number, right: number): number;
       subtract(left: number, right: number): number;
       multiply(left: number, right: number): number;
       divide(left: number, right: number): number;
   }

Type declaration files can be written by hand for existing JavaScript libraries, as has been done for jQuery and Node.js.

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 that integrate the optional type annotations support.

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.

There is also a client-side compiler in JavaScript, which executes TypeScript code on the fly, upon page load[6].

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. Classes, despite being 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 [7]

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

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.[9]

See also

References

External links

Personal tools
  • Create account
  • Log in
Namespaces

Variants
Actions
Navigation
Toolbox
Print/export