Use Union Types

Use Union Types Don’t write overloads that differ by type in only one argument position: /* WRONG */ interface Moment { utcOffset(): number; utcOffset(b: number): Moment; utcOffset(b: string): Moment; } Do use union types whenver possible: /* OK */ interface Moment { utcOffset(): number; utcOffset(b: number|string): Moment; } Note that we didn’t make b optional here because the return types of the signatures differ. Why: This is important for people who are “passing through” a value

Integrating with Build Tools

Integrating with Build Tools You might have some more build steps in your pipeline. Perhaps you concatenate something to each of your files. Each build tool is different, but we’ll do our best to cover the gist of things.

Add TypeScript code

Add TypeScript code Right click on src and click New Item. Then choose TypeScript File and name the file app.ts.

Advanced Combinations

Advanced Combinations Some kinds of declarations can be combined across multiple declarations. For example, class C { } and interface C { } can co-exist and both contribute properties to the C types. This is legal as long as it does not create a conflict. A general rule of thumb is that values always conflict with other values of the same name unless they are declared as namespaces, types will conflict if they are declared with a type alias declaration (type s = string), and namespaces never co

Consuming Dependencies

Consuming Dependencies There are several kinds of dependencies you might have.

Dependencies on Global Libraries

Dependencies on Global Libraries If your library depends on a global library, use a /// <reference types="..." /> directive: /// <reference types="someLib" /> function getThing(): someLib.thing;

Merging Namespaces with Classes

Merging Namespaces with Classes This gives the user a way of describing inner classes. class Album { label: Album.AlbumLabel; } namespace Album { export class AlbumLabel { } } The visibility rules for merged members is the same as described in the ‘Merging Namespaces’ section, so we must export the AlbumLabel class for the merged class to see it. The end result is a class managed inside of another class. You can also use namespaces to add more static members to an existing class. In additi

Publish to npm

Publish to npm The Publishing section explains how to publish your declaration files to an npm package, and shows how to manage your dependent packages.

Function declarations

Function declarations Destructuring also works in function declarations. For simple cases this is straightforward: type C = {a: string, b?: number} function f({a, b}: C): void { // ... } But specifying defaults is more common for parameters, and getting defaults right with destructuring can be tricky. First of all, you need to remember to put the type before the default value. function f({a, b} = {a: "", b: 0}): void { // ... } f(); // ok, default to {a: "", b: 0} Then, you need to rememb

Write a simple Angular app in TypeScript

Write a simple Angular app in TypeScript First, change the code in app.ts to: import {Component} from "angular2/core" import {MyModel} from "./model" @Component({ selector: `my-app`, template: `<div>Hello from </div>` }) class MyApp { model = new MyModel(); getCompiler() { return this.model.compiler; } } Then add another TypeScript file in src named model.ts: export class MyModel { compiler = "TypeScript"; } And then another TypeScript file in src named main.ts: i