Introduction

These examples are ordered in approximately increasing order of complexity.

The Examples

Documentation

Code

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.

  1. /** The number of widgets present */
  2. declare var foo: number;

Global Functions

Documentation

You can call the function greet with a string to show a greeting to the user.

Code

  1. greet("hello, world");

Declaration

Use declare function to declare functions.

  1. declare function greet(greeting: string): void;

Documentation

Code

  1. let result = myLib.makeGreeting("hello, world");
  2. console.log("The computed greeting is:" + result);

Use declare namespace to describe types or values accessed by dotted notation.

Overloaded Functions

Documentation

The getWidget function accepts a number and returns a Widget, or accepts a string and returns a Widget array.

Code

  1. let x: Widget = getWidget(43);
  2. let arr: Widget[] = getWidget("all of them");

Declaration

  1. declare function getWidget(n: number): Widget;
  2. declare function getWidget(s: string): Widget[];

Documentation

When specifying a greeting, you must pass a GreetingSettings object.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

  1. greet({
  2. duration: 4000
  3. });

Declaration

Use an interface to define a type with properties.

  1. interface GreetingSettings {
  2. greeting: string;
  3. duration?: number;
  4. color?: string;
  5. }
  6. declare function greet(setting: GreetingSettings): void;

Reusable Types (Type Aliases)

Documentation

Code

Declaration

You can use a type alias to make a shorthand for a type:

  1. type GreetingLike = string | (() => string) | Greeter;
  2. declare function greet(g: GreetingLike): void;

Documentation

The greeter object can log to a file or display an alert.You can provide LogOptions to .log(…) and alert options to .alert(…)

Code

  1. const g = new Greeter("Hello");
  2. g.log({ verbose: true });
  3. g.alert({ modal: false, title: "Current Greeting" });

Declaration

Use namespaces to organize types.

  1. declare namespace GreetingLib {
  2. interface LogOptions {
  3. interface AlertOptions {
  4. modal: boolean;
  5. title?: string;
  6. color?: string;
  7. }
  8. }

You can also create nested namespaces in one declaration:

  1. declare namespace GreetingLib.Options {
  2. // Refer to via GreetingLib.Options.Log
  3. interface Log {
  4. verbose?: boolean;
  5. }
  6. interface Alert {
  7. modal: boolean;
  8. title?: string;
  9. color?: string;
  10. }
  11. }

Classes

Documentation

Code

Declaration

Use declare class to describe a class or class-like object.Classes can have properties and methods as well as a constructor.

  1. declare class Greeter {
  2. constructor(greeting: string);
  3. greeting: string;
  4. showGreeting(): void;
  5. }