声明参考手册
本指南的目的是教您如何编写高质量的定义文件。本指南的结构是展示一些 API 的文档,以及该 API 的示例用法,并解释如何编写相应的声明。
这些示例按复杂程度大致递增的顺序排列。
具有属性的对象
文档
全局变量
myLib
有一个函数makeGreeting
用于创建问候语,还有一个属性numberOfGreetings
表示到目前为止问候语的数量。
代码
let result = myLib.makeGreeting("hello, world");
console.log("The computed greeting is:" + result);
let count = myLib.numberOfGreetings;
4K/XdksJKTk/0rFZdwk+1Q==
RqRRpnjzd/umSS/jITiZVAZAF2WUFbZBK/c1k5imWkEqAV413OBOwqQwx3jSOjGRqef/dRUCProMJlmfo0LAMKLdyKs5qxULnOGiO+lWR0JvjkFLQ3G5suemOSCmyPXf
declare namespace myLib {
function makeGreeting(s: string): string;
let numberOfGreetings: number;
}
重载函数
u8Gy8EPuf0ROQ0ojzJnWFQ==
FBfCkyHs7jHasynCbwF0WyE6Kfyk+r+ELw127GGFgME/iZ1CWJqBmZphoX/xD4A2RvX0vDzHJRACVUQ+xKSRMbGoDM+kjkNZAFPzQAPAlNM8KuE9g9RXVcRBLd0vBngHeuMiKHft4gyXWO7jykPcInyOvoY63Un/bfm4sI9KCWh+oGhUoOLcIyl9LlnBOQ8V
h4y6nDAnH9a+CULNOCdCZg==
let x: Widget = getWidget(43);
let arr: Widget[] = getWidget("all of them");
4K/XdksJKTk/0rFZdwk+1Q==
declare function getWidget(n: number): Widget;
declare function getWidget(s: string): Widget[];
可重用类型(接口)
u8Gy8EPuf0ROQ0ojzJnWFQ==
e6nX70u7GAx/fWAA0V17oPunqhz3TzwiT0HQWnE++wRkJwn5k3TTvjrCOz6b0W3VkznwBJYXxQcfMsWbdD0tW0aD02l9JzGEBwF5PoirI9hntnimdq0nlARzqRIaRmv3ignjXJj9O39qLFV4F4q2CA==
prEPjk5hYrE/LtOd2CC/ccNBPfTP0lEWsfrFcSkv6so=
SbNF09KQuE3mVNKYcWO1eyZWEPJC2cExrHOAEnzfTeK/4F8B/+JQZbgt2ouTOfxFYcnuAU3BEZ4DhRBwYjkHlg==
hXxIIiWegwbigPJpGqYZ1AdFKBlWlRXbbLiWrcAHcAQDqDEcuF/Va4y2sEvu1kup
h4y6nDAnH9a+CULNOCdCZg==
greet({
greeting: "hello world",
duration: 4000
});
4K/XdksJKTk/0rFZdwk+1Q==
mmENXfDI3va7L79DZkCTGJ5vl1hnqjWKXBGCozaqr8bTKG4brBJvhS/dE+2K5WOsE5MQwH0Z15yntmg/T4Kcjw==
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
declare function greet(setting: GreetingSettings): void;
可重用类型(类型别名)
u8Gy8EPuf0ROQ0ojzJnWFQ==
PcveUPBBOLieVGzg2wrgJttT+t6LIes0eofJI4su9CHX2yVQexym5+0JES3X7+lPq/mRwGx+/x1XZadyRJErbxy9StGiTCBTP4O/T+j4zZ5/1+tI3sNKz4rk2AD687nxbYbxusLMYSDF5l0u3WWtGAFeD8E1jYDVR2knyZNJl18kgez4TBnqJc+bfv1FCKLa81EHsNxWjqk5TzgcN1mVxg==
h4y6nDAnH9a+CULNOCdCZg==
function getGreeting() {
return "howdy";
}
class MyGreeter extends Greeter {}
greet("hello");
greet(getGreeting);
greet(new MyGreeter());
4K/XdksJKTk/0rFZdwk+1Q==
XfXh03w+UIiq6McXRvRT2Ry0Qksmp4T06pLJ/A+1j30/kZbQTet4mx63WgRfprVR
type GreetingLike = string | (() => string) | MyGreeter;
declare function greet(g: GreetingLike): void;
组织类型
u8Gy8EPuf0ROQ0ojzJnWFQ==
N+qOrdr46+EIZ2NWy1n7g3eRqzBbR9Ub7g96mRDibbZBVujBkhRSYG1rCfhlOffUJBRMY4fIociVCsKAQu2eCdxLBPnWsl8mG9Nfc6/hV/QvBCiXZaGmOJQPSr5jlgAnl8x+D9ZoQmyIXSeJBeFoqZYxe2xQ0KhWnH4YAVUYkxOwgWyfOFqUmW9gjMQNvAJdOUNUjuaiHCcIedusD7uTIneHi2ux11WKSSVsPH/sL7s=
h4y6nDAnH9a+CULNOCdCZg==
const g = new Greeter("Hello");
g.log({ verbose: true });
g.alert({ modal: false, title: "Current Greeting" });
4K/XdksJKTk/0rFZdwk+1Q==
O3FuBxQp80/ZI7olo7KMTudbcsuVXjYF+RC2ldQv6DGInxJUCrFG+6ttYoGU+Ege
declare namespace GreetingLib {
interface LogOptions {
verbose?: boolean;
}
interface AlertOptions {
modal: boolean;
title?: string;
color?: string;
}
}
hhvPM3RuUmNCK8MRxQy0N9sNT0ZHyGZGGrNhK2B99TqdBL8K1mFRDhOzb+SNZyl2UwF0l7T8gx6w18AeBKke/A==
declare namespace GreetingLib.Options {
// Refer to via GreetingLib.Options.Log
interface Log {
verbose?: boolean;
}
interface Alert {
modal: boolean;
title?: string;
color?: string;
}
}
类
u8Gy8EPuf0ROQ0ojzJnWFQ==
xhN49FTQ+wmsBaI8UmR1+fEsdj5VzWpmusxAX4+ZFD9DctIEqkkxMuvf5Xl8YqLHja5Qu/K5MBwwgYe/7T1ycpDK3fu2dhI55oi5WOT8s429e9Lpi3W+9OhYO3kJ6pVDkp+Aa9QubWk000HUc+iHjY8kBwePRBofXj+ShsEHk6RK6ycIaOCy+D7LWLBIM+XW
h4y6nDAnH9a+CULNOCdCZg==
const myGreeter = new Greeter("hello, world");
myGreeter.greeting = "howdy";
myGreeter.showGreeting();
class SpecialGreeter extends Greeter {
constructor() {
super("Very special greetings");
}
}
4K/XdksJKTk/0rFZdwk+1Q==
RqRRpnjzd/umSS/jITiZVBamD6GrZkm27yyLAVPMttvIvq1MqMcfeHzPVxTupQZbXSDc3IFXaxj0C22xXysIibAk5Fkf6kC/up4RodSPcuEW7VwwAQ3SssRPl295+KASW2i068w63WJqvN1u0qqRlj959lcwU+K/xjd10NPuxKs=
declare class Greeter {
constructor(greeting: string);
greeting: string;
showGreeting(): void;
}
全局变量
u8Gy8EPuf0ROQ0ojzJnWFQ==
cv8j286YE9MBzRx/54RsdtFgtdj227GX4Xr3f6qAAN7T1lvN/3wD8H/143WD2uxbXQwFOxB/6GujZmVMTfNkeQ==
h4y6nDAnH9a+CULNOCdCZg==
console.log("Half the number of widgets is " + foo / 2);
4K/XdksJKTk/0rFZdwk+1Q==
RqRRpnjzd/umSS/jITiZVIqqRUVOBDX2H8LfsENUPgxe7bGZ3Nfdh382AK4nWt+k7DzaACNCVrr3SzLYP2QSAcqW0i8rLwQ2He829+9oGJHrILHnoSVwRXYis+otEBiHH1xtKnpv0G7jotH2N52vEqN6D4aHrQghlAzeckSXmsP8A5U3XV+xSpmQYc96T14ZS3/yoD+D1qZp1WftvBnhhWioYQRK5xQ7jnwYcXya3acKNn+7kbYgh5kNBE+M3WoBUr/l/2QPMbigFrHov12Y7Q==
/** The number of widgets present */
declare var foo: number;
全局函数
u8Gy8EPuf0ROQ0ojzJnWFQ==
PaAci1QTydfx34yCXf8vXvDQEoNKCvpvP0n9Ti/+VXMVfLu+J5cE1+9EwDDMGuSeZR22Ciz8Ld2McJsTorZNzcEc+WeV+XAyphizaq97b/5IZbi3sKiQ80blEL/Cnpdr
h4y6nDAnH9a+CULNOCdCZg==
greet("hello, world");
4K/XdksJKTk/0rFZdwk+1Q==
RqRRpnjzd/umSS/jITiZVGYCOFlt+lNz90FYkYzomGvOVQa2hEOJrPlQxInFnVpruMUpE6Rbg+uWOLV+YegKXw==
declare function greet(greeting: string): void;