Declaration Reference
TypeScript Handbook · نسخهٔ cf57c7ec6762 · مجوزهای مخزن رسمی TypeScript-Website
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 function makeGreeting for creating greetings, and a property numberOfGreetings indicating the number of greetings made so far.
_Code_
let 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.
declare 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_
let x: Widget = getWidget(43);
let arr: Widget[] = getWidget("all of them");_Declaration_
declare function getWidget(n: number): Widget;
declare function getWidget(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_
greet({
greeting: "hello world",
duration: 4000
});_Declaration_
Use an interface to define a type with properties.
interface 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 a string, or a Greeter instance.
_Code_
function 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:
type GreetingLike = string | (() => string) | MyGreeter;
declare function greet(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_
const g = new Greeter("Hello");
g.log({ verbose: true });
g.alert({ modal: false, title: "Current Greeting" });_Declaration_
Use namespaces to organize types.
declare namespace GreetingLib {
interface LogOptions {
verbose?: boolean;
}
interface AlertOptions {
modal: boolean;
title?: string;
color?: string;
}
}You can also create nested namespaces in one declaration:
declare namespace GreetingLib.Options {
// Refer to via GreetingLib.Options.Log
interface Log {
verbose?: boolean;
}
interface Alert {
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_
const 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.
declare class Greeter {
constructor(greeting: string);
greeting: string;
showGreeting(): void;
}Global Variables
_Documentation_
توجه: The global variable foo contains the number of widgets present.
_Code_
console.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.
/** The number of widgets present */
declare var foo: number;Global Functions
_Documentation_
توجه: You can call the function greet with a string to show a greeting to the user.
_Code_
greet("hello, world");_Declaration_
Use declare function to declare functions.
declare function greet(greeting: string): void;