The purpose of this guide is to teach you how to write a high-quality definition file. This guide is structured by showing documentation for some API, along with sample usage of that API, and explaining how to write the corresponding declaration.
These examples are ordered in approximately increasing order of complexity.
Objects with Properties
Documentation
The global variable
myLibhas a functionmakeGreetingfor creating greetings, and a propertynumberOfGreetingsindicating the number of greetings made so far.
Code
tslet result = myLib.makeGreeting("hello, world");console.log("The computed greeting is:" + result);let count = myLib.numberOfGreetings;
Declaration
Use declare namespace to describe types or values accessed by dotted notation.
tsdeclare namespace myLib {function makeGreeting(s: string): string;let numberOfGreetings: number;}
Overloaded Functions
Documentation
The getWidget function accepts a number and returns a Widget, or accepts a string and returns a Widget array.
Code
tslet x: Widget = getWidget(43);let arr: Widget[] = getWidget("all of them");
Declaration
tsdeclare function getWidget(n: number): Widget;declare function getWidget(s: string): Widget[];
Reusable Types (Interfaces)
Documentation
When specifying a greeting, you must pass a
GreetingSettingsobject. This object has the following properties:1 - greeting: Mandatory string
2 - duration: Optional length of time (in milliseconds)
3 - color: Optional string, e.g. ‘#ff00ff’
Code
tsgreet({greeting: "hello world",duration: 4000});
Declaration
Use an interface to define a type with properties.
tsinterface GreetingSettings {greeting: string;duration?: number;color?: string;}declare function greet(setting: GreetingSettings): void;
Reusable Types (Type Aliases)
Documentation
Anywhere a greeting is expected, you can provide a
string, a function returning astring, or aGreeterinstance.
Code
tsfunction getGreeting() {return "howdy";}class MyGreeter extends Greeter {}greet("hello");greet(getGreeting);greet(new MyGreeter());
Declaration
You can use a type alias to make a shorthand for a type:
tstype GreetingLike = string | (() => string) | MyGreeter;declare function greet(g: GreetingLike): void;
Organizing Types
Documentation
The
greeterobject can log to a file or display an alert. You can provide LogOptions to.log(...)and alert options to.alert(...)
Code
tsconst g = new Greeter("Hello");g.log({ verbose: true });g.alert({ modal: false, title: "Current Greeting" });
Declaration
Use namespaces to organize types.
tsdeclare namespace GreetingLib {interface LogOptions {verbose?: boolean;}interface AlertOptions {modal: boolean;title?: string;color?: string;}}
You can also create nested namespaces in one declaration:
tsdeclare namespace GreetingLib.Options {// Refer to via GreetingLib.Options.Loginterface Log {verbose?: boolean;}interface Alert {modal: boolean;title?: string;color?: string;}}
Classes
Documentation
You can create a greeter by instantiating the
Greeterobject, or create a customized greeter by extending from it.
Code
tsconst myGreeter = new Greeter("hello, world");myGreeter.greeting = "howdy";myGreeter.showGreeting();class SpecialGreeter extends Greeter {constructor() {super("Very special greetings");}}
Declaration
Use declare class to describe a class or class-like object.
Classes can have properties and methods as well as a constructor.
tsdeclare class Greeter {constructor(greeting: string);greeting: string;showGreeting(): void;}
Global Variables
Documentation
The global variable
foocontains the number of widgets present.
Code
tsconsole.log("Half the number of widgets is " + foo / 2);
Declaration
Use declare var to declare variables.
If the variable is read-only, you can use declare const.
You can also use declare let if the variable is block-scoped.
ts/** The number of widgets present */declare var foo: number;
Global Functions
Documentation
You can call the function
greetwith a string to show a greeting to the user.
Code
tsgreet("hello, world");
Declaration
Use declare function to declare functions.
tsdeclare function greet(greeting: string): void;