TypeScript 1.4

Union types Overview Union types are a powerful way to express a value that can be one of several types. For example, you might have an API for running a program that takes a commandline as either a string, a string[] or a function that returns a string. You can now write: interface RunOptions { program: string; commandline: string[]|string|(() => string); } Assignment to union types works very intuitively – anything you could assign to one of the union type’s members is assignable to

Attribute type checking

Attribute type checking The first step to type checking attributes is to determine the element attributes type. This is slightly different between intrinsic and value-based elements. For intrinsic elements, it is the type of the property on JSX.IntrinsicElements declare namespace JSX { interface IntrinsicElements { foo: { bar?: boolean } } } // element attributes type for 'foo' is '{bar?: boolean}' <foo bar />; For value-based elements, it is a bit more complex. It is determined b

Schema

Schema Schema can be found at: http://json.schemastore.org/tsconfig

Details

Details The "compilerOptions" property can be omitted, in which case the compiler’s defaults are used. See our full list of supported Compiler Options. The "files" property takes a list of relative or absolute file paths. The "include" and "exclude" properties take a list of glob-like file patterns. The supported glob wildcards are: * matches zero or more characters (excluding directory separators) ? matches any one character (excluding directory separators) **/ recursively matches any subdi

Downloading

Downloading Getting type declarations in TypeScript 2.0 and above requires no tools apart from npm. As an example, getting the declarations for a library like lodash takes nothing more than the following command npm install --save @types/lodash

Global augmentation

Global augmentation You can also add declarations to the global scope from inside a module: // observable.ts export class Observable<T> { // ... still no implementation ... } declare global { interface Array<T> { toObservable(): Observable<T>; } } Array.prototype.toObservable = function () { // ... } Global augmentations have the same behavior and limits as module augmentations.

Object destructuring

Object destructuring You can also destructure objects: let o = { a: "foo", b: 12, c: "bar" } let {a, b} = o; This creates new variables a and b from o.a and o.b. Notice that you can skip c if you don’t need it. Like array destructuring, you can have assignment without declaration: ({a, b} = {a: "baz", b: 101}); Notice that we had to surround this statement with parentheses. JavaScript normally parses a { as the start of block.

this parameters

this parameters Unfortunately, the type of this.suits[pickedSuit] is still any. That’s because this comes from the function expression inside the object literal. To fix this, you can provide an explicit this parameter. this parameters are fake parameters that come first in the parameter list of a function: function f(this: void) { // make sure `this` is unusable in this standalone function } Let’s add a couple of interfaces to our example above, Card and Deck, to make the types clearer and

global.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~] // Project: [~THE PROJECT NAME~] // Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]> /*~ If this library is callable (e.g. can be invoked as myLib(3)), *~ include those call signatures here. *~ Otherwise, delete this section. */ declare function myLib(a: string): string; declare function myLib(a: number): number; /*~ If you want the name of this library to be a valid type name, *~ you can do so here. *~ *~ F

global-plugin.d.ts

// Type definitions for [~THE LIBRARY NAME~] [~OPTIONAL VERSION NUMBER~] // Project: [~THE PROJECT NAME~] // Definitions by: [~YOUR NAME~] <[~A URL FOR YOU~]> /*~ This template shows how to write a global plugin. */ /*~ Write a declaration for the original type and add new members. *~ For example, this adds a 'toBinaryString' method with to overloads to *~ the built-in number type. */ interface Number { toBinaryString(opts?: MyLibrary.BinaryFormatOptions): string; toBinaryString(