CoffeeScript, Dart, Elm, and TypeScript

TypeScript

Microsoft has also worked for several years on a JavaScript alternative named TypeScript [15]. JavaScript libraries like jQuery can still be used in TypeScript, which also adds proprietary constructs to the latest version of ECMAScript, much like CoffeeScript. Today, elements from TypeScript are regularly fed back into the official ECMAScript standard. Microsoft offers a compiler for TypeScript [16] that translates TypeScript programs into ECMAScript. Like the alternative scripting languages, you can test TypeScript programs in an online editor – the Playground editor (Figure 4) [17] in this case.

Figure 4: TypeScript examples are available in the Playground editor. The Using Classes example demonstrates the handling of classes.

As in Dart, you need to set the variable type; the compiler then performs a static type check. In the following example, the second line defines a function; the return type follows the name:

var name: string = "Tim";
function output: void (a: string) { console.log(a); }

The void() return type tells the compiler that the function returns nothing. The special return type never defines functions that do not terminate, as is the case with exceptions. In addition to strings, TypeScript supports Boolean values (boolean) and floating-point numbers (number).

If the type of a variable is unknown, you explicitly assign the any type, which tells the compiler to switch off type checking. Conversely, you can check the type explicitly two ways:

var length: any = (<string>firstname).length;
var length: any = (firstname as string).length;

In functions, you can allow several alternative types for a single parameter. The following example passes in either a number or a string:

function foo(value: string | number) { ... }

You can also dictate the exact values that the string is allowed to contain (string literal types), which is useful to enforce certain settings in web applications. Like Dart, TypeScript supports the enumerated type enum:

enum Color {Red, Yellow, Blue};

Tuples are an alternative. In the following example, workers stores two values: John and 34. This pair of values can be accessed with the use of an index, as with an array:

var workers: var workers:
workers = ["John", 34].
workers[1] = 35;

Classes are defined as in ES2015. On request, the TypeScript compiler translates them to older ECMAScript. If a property or a method is preceded by the private keyword, only the methods from the class are allowed to access it. In the case of protected, however, all derived classes also have access.

The static keyword tells TypeScript to create properties that all objects of a class share. TypeScript adds notation for getter and setter methods:

class Auto {
   protected _model: string;
   get model(): string { return this. _model; }}

TypeScript adds interfaces to the class concept that describe only the structure or the interface of an object (e.g., Listing 4). A property that is marked with a question mark is optional. The extends keyword lets an interface extend another interface or class; for classes, the interface only adopts the methods and properties.

Listing 4

Example of Interfaces in TypeScript

 

In TypeScript, two types are compatible with each other if their internal structure is the same. Unlike other languages, then, you do not need to implement the interface explicitly. It is sufficient if an object provides the interface described. Listing 4 shows an example in which sayHello() correctly processes an Employee. An interface describes functions as well as classes. In addition to interfaces are abstract classes, which only let you derive other classes.

In the case of objects created with const, you can also change the properties of the object. This is only prevented by the readonly keyword preceding the corresponding property. Indexable types transform an interface into an array or a dictionary.

In the following example, the Employees interface comprises several names:

interface Employees {}
        [index: number]: string;
}

Access is as in an array. Here, the index is a number, and the stored values are text (string). You replace number with string to access the stored values by way of terms (e.g., zip["Lawrence"]).

In some cases, TypeScript groups several declarations to form a single declaration (declaration merging). In the following example, the compiler automatically merges the two interface declarations:

interface Dot { x: number; }
interface Dot { y: number; }
let p: Dot = {x: 1, y: 2};

TypeScript also offers generics, with notation similar to Java or Dart. Mixins compile several classes to create a new one, and intersection types allow combining of types. For example, Person & Employee & Student is a person, and an employee, and a student.

TypeScript supports ECMAScript modules and produces code that is suitable for modular solutions from Node.js (CommonJS), RequireJS (AMD), Isomorphic (UMD), and SystemJS. TypeScript encapsulates variables, classes, and other elements in its own namespace on request, reducing the risk that an imported function uses the same name as your own function.

If you enjoy experiments, you can already use decorators in the current version. They give a class, property, or method additional properties. The example assigns the decorator @sealed to the class foo:

@sealed
class Foo { ... }

What this modification actually does is determined in a private function. The TypeScript compiler supports the JSX specification [18] and points out typical JavaScript errors. Plugins integrate TypeScript with multiple build tools such as Maven. Plugins exist for Atom, Eclipse, and many other editors and IDEs. The compiler itself is written in TypeScript and needs Node.js to execute.

The tools developed by Microsoft and the compiler itself are available under the Apache 2.0 license; the language specification is under the Open Web Foundation Final Specification Agreement Version 1.0 (OWF 1.0) [19] open license.

Conclusions

The choice between CoffeeScript, Dart, Elm, and TypeScript depends on the project in question, your requirements, and your own preferences. CoffeeScript is slightly out of touch with ECMAScript and does not offer type checking. The advantage of this scripting language is thus limited to Python-style code formatting with tabs and the ability to mix Markdown with CoffeeScript.

Dart can be run in a special VM and offers modern object-oriented concepts, such as mixins, but it is oriented more on Java and C than on JavaScript.

Elm specifically targets fans of functional programming who can develop web applications using familiar patterns of thought. Thanks to automatic type deduction, run-time errors are also rare in Elm.

TypeScript is now used as a playground for future versions of ECMAScript. Because it is based on JavaScript, making the move is easy, and typing ensures fewer run-time errors. Dart and TypeScript let you mix typed and typeless variables, so despite compiler support, a small error source remains.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • CoffeeScript

    Through the years, many languages have tried to improve or even replace JavaScript, including CoffeeScript, which runs in all JavaScript Runtime Environments and compiles into JavaScript.

  • Google Dart

    The Dart programming language is Google’s modern alternative to JavaScript. It will mainly run in browsers but can also be used at the command line and on servers as a replacement for PHP.

  • Google Web Toolkit

    The Ingenious Google Web Toolkit builds optimized JavaScript applications in a hurry.

  • MathLex

    MathLex lets you easily transform handwritten math formulas to digital format and use them on the web.

  • Helma

    The powerful Helma application server brings new powers to the JavaScript language. We'll show you how to use Helma to build a simple RSS reader.

comments powered by Disqus