Import

Import Importing is just about as easy as exporting from a module. Importing an exported declaration is done through using one of the import forms below:

Indexable Types

Indexable Types Similarly to how we can use interfaces to describe function types, we can also describe types that we can “index into” like a[10], or ageMap["daniel"]. Indexable types have an index signature that describes the types we can use to index into the object, along with the corresponding return types when indexing. Let’s take an example: interface StringArray { [index: number]: string; } let myArray: StringArray; myArray = ["Bob", "Fred"]; let myStr: string = myArray[0]; Above, w

Identifying Kinds of Libraries

Identifying Kinds of Libraries First, we’ll review the kinds of libraries TypeScript declaration files can represent. We’ll briefly show how each kind of library is used, how it is written, and list some example libraries from the real world. Identifying the structure of a library is the first step in writing its declaration file. We’ll give hints on how to identify structure both based on its usage and its code. Depending on the library’s documentation and organization, one might be easier tha

Inferring the types

Inferring the types In playing with the example, you may notice that the TypeScript compiler can figure out the type if you have types on one side of the equation but not the other: // myAdd has the full function type let myAdd = function(x: number, y: number): number { return x + y; }; // The parameters 'x' and 'y' have the type number let myAdd: (baseValue:number, increment:number) => number = function(x, y) { return x + y; }; This is called “contextual typing”, a form of type inferen

Import a single export from a module

Import a single export from a module import { ZipCodeValidator } from "./ZipCodeValidator"; let myValidator = new ZipCodeValidator(); imports can also be renamed import { ZipCodeValidator as ZCV } from "./ZipCodeValidator"; let myValidator = new ZCV();

hasInstance

Symbol.hasInstance A method that determines if a constructor object recognizes an object as one of the constructor’s instances. Called by the semantics of the instanceof operator.

Gulp

Gulp If you’re using Gulp in some fashion, we have a tutorial on using Gulp with TypeScript, and integrating with common build tools like Browserify, Babelify, and Uglify. You can read more there.

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

Grunt

Grunt

gulp

gulp