Create a new project

Create a new project Choose File Choose New Project Choose Visual C# Choose ASP.NET Web Application Choose MVC I unchecked “Host in the cloud” since this will be a local demo. Run the application and make sure that it works.

Preventing Name Conflicts

Preventing Name Conflicts Note that it’s possible to define many types in the global scope when writing a global declaration file. We strongly discourage this as it leads to possible unresolvable name conflicts when many declaration files are in a project. A simple rule to follow is to only declare types namespaced by whatever global variable the library defines. For example, if the library defines the global value ‘cats’, you should write declare namespace cats { interface KittySettings { }

Type Aliases

Type Aliases Type aliases create a new name for a type. Type aliases are sometimes similar to interfaces, but can name primitives, unions, tuples, and any other types that you’d otherwise have to write by hand. type Name = string; type NameResolver = () => string; type NameOrResolver = Name | NameResolver; function getName(n: NameOrResolver): Name { if (typeof n === "string") { return n; } else { return n(); } } Aliasing doesn’t actually create a new type - it creates a new

Optional Parameters in Callbacks

Optional Parameters in Callbacks Don’t use optional parameters in callbacks unless you really mean it: /* WRONG */ interface Fetcher { getObject(done: (data: any, elapsedTime?: number) => void): void; } This has a very specific meaning: the done callback might be invoked with 1 argument or might be invoked with 2 arguments. The author probably intended to say that the callback might not care about the elapsedTime parameter, but there’s no need to make the parameter optional to accomplish

instanceof type guards

instanceof type guards If you’ve read about typeof type guards and are familiar with the instanceof operator in JavaScript, you probably have some idea of what this section is about. instanceof type guards are a way of narrowing types using their constructor function. For instance, let’s borrow our industrial string-padder example from earlier: interface Padder { getPaddingString(): string } class SpaceRepeatingPadder implements Padder { constructor(private numSpaces: number) { } getPad

toStringTag

Symbol.toStringTag A String value that is used in the creation of the default string description of an object. Called by the built-in method Object.prototype.toString.

Classes

Classes Classes work similarly to object literal types and interfaces with one exception: they have both a static and an instance type. When comparing two objects of a class type, only members of the instance are compared. Static members and constructors do not affect compatibility. class Animal { feet: number; constructor(name: string, numFeet: number) { } } class Size { feet: number; constructor(numFeet: number) { } } let a: Animal; let s: Size; a = s; //OK s = a; //OK

Disallowed Merges

Disallowed Merges Not all merges are allowed in TypeScript. Currently, classes can not merge with other classes or with variables. For information on mimicking class merging, see the Mixins in TypeScript section.

Definition File Theory: A Deep Dive

Definition File Theory: A Deep Dive Structuring modules to give the exact API shape you want can be tricky. For example, we might want a module that can be invoked with or without new to produce different types, has a variety of named types exposed in a hierarchy, and has some properties on the module object as well. By reading this guide, you’ll have the tools to write complex definition files that expose a friendly API surface. This guide focuses on module (or UMD) libraries because the optio

compileOnSave

compileOnSave Setting a top-level property compileOnSave signals to the IDE to generate all files for a given tsconfig.json upon saving. { "compileOnSave": true, "compilerOptions": { "noImplicitAny" : true } } This feature is currently supported in Visual Studio 2015 with TypeScript 1.8.4 and above, and atom-typescript plugin.