Declaration Reference
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
myLib
has a functionmakeGreeting
for creating greetings,and a propertynumberOfGreetings
indicating the number of greetings made so far.
Code
ts
letresult =myLib.makeGreeting("hello, world");console.log("The computed greeting is:" +result);letcount =myLib.numberOfGreetings;
Declaration
Usedeclare namespace
to describe types or values accessed by dotted notation.
ts
declarenamespacemyLib {functionmakeGreeting(s:string):string;letnumberOfGreetings:number;}
Overloaded Functions
Documentation
ThegetWidget
function accepts a number and returns a Widget, or accepts a string and returns a Widget array.
Code
ts
letx:Widget =getWidget(43);letarr:Widget[] =getWidget("all of them");
Declaration
ts
declarefunctiongetWidget(n:number):Widget;declarefunctiongetWidget(s:string):Widget[];
Reusable Types (Interfaces)
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
ts
greet({greeting:"hello world",duration:4000});
Declaration
Use aninterface
to define a type with properties.
ts
interfaceGreetingSettings {greeting:string;duration?:number;color?:string;}declarefunctiongreet(setting:GreetingSettings):void;
Reusable Types (Type Aliases)
Documentation
Anywhere a greeting is expected, you can provide a
string
, a function returning astring
, or aGreeter
instance.
Code
ts
functiongetGreeting() {return"howdy";}classMyGreeterextendsGreeter {}greet("hello");greet(getGreeting);greet(newMyGreeter());
Declaration
You can use a type alias to make a shorthand for a type:
ts
typeGreetingLike =string | (()=>string) |MyGreeter;declarefunctiongreet(g:GreetingLike):void;
Organizing Types
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
ts
constg =newGreeter("Hello");g.log({verbose:true });g.alert({modal:false,title:"Current Greeting" });
Declaration
Use namespaces to organize types.
ts
declarenamespaceGreetingLib {interfaceLogOptions {verbose?:boolean;}interfaceAlertOptions {modal:boolean;title?:string;color?:string;}}
You can also create nested namespaces in one declaration:
ts
declarenamespaceGreetingLib.Options {// Refer to via GreetingLib.Options.LoginterfaceLog {verbose?:boolean;}interfaceAlert {modal:boolean;title?:string;color?:string;}}
Classes
Documentation
You can create a greeter by instantiating the
Greeter
object, or create a customized greeter by extending from it.
Code
ts
constmyGreeter =newGreeter("hello, world");myGreeter.greeting ="howdy";myGreeter.showGreeting();classSpecialGreeterextendsGreeter {constructor() {super("Very special greetings");}}
Declaration
Usedeclare class
to describe a class or class-like object.Classes can have properties and methods as well as a constructor.
ts
declareclassGreeter {constructor(greeting:string);greeting:string;showGreeting():void;}
Global Variables
Documentation
The global variable
foo
contains the number of widgets present.
Code
ts
console.log("Half the number of widgets is " +foo /2);
Declaration
Usedeclare var
to declare variables.If the variable is read-only, you can usedeclare const
.You can also usedeclare let
if the variable is block-scoped.
ts
/** The number of widgets present */declarevarfoo:number;
Global Functions
Documentation
You can call the function
greet
with a string to show a greeting to the user.
Code
ts
greet("hello, world");
Declaration
Usedeclare function
to declare functions.
ts
declarefunctiongreet(greeting:string):void;
The TypeScript docs are an open source project. Help us improve these pagesby sending a Pull Request ❤
Last updated: Oct 06, 2025