深入探讨
声明文件理论:深入探讨
构建模块以提供您想要的确切 API 形状可能很棘手。例如,我们可能希望一个模块可以使用或不使用 new
来调用以生成不同的类型,在层次结构中公开各种命名类型,并且在模块对象上也有一些属性。
通过阅读本指南,您将拥有编写复杂声明文件的工具,这些文件公开了友好的 API 表面。本指南重点介绍模块(或 UMD)库,因为此处的选项更加多样。
关键概念
通过了解 TypeScript 工作原理的一些关键概念,您可以完全理解如何进行任何形式的声明。
类型
如果您正在阅读本指南,您可能已经大致了解 TypeScript 中的类型是什么。不过,更明确地说,引入了一个类型:
- 类型别名声明 (
type sn = number | string;
) - 接口声明(
interface I { x: number[]; }
) - 类声明(
class C { }
) - 枚举声明 (
enum E { A, B, C }
) - 引用类型的
import
声明
//Xoyz9CXGaY0iBmugq9jKBrg7RQqIDwTfIeyDUn02l7CSvTbNpkx2y2S9SskdJypjUxIPMwCeSjnDRM+P31svDnkvKwK3LUqKDiYewCZlg=
值
SY1tu6ORlWz2+CE6NyCGZaxGpDQoaJk3DcNfPLA8GEoPinwgzw8rrUV+A0lhvBiaFeByZtYR9FV9Bn0JVFtDq8t5DbcrEuOPYncdxfR/TXE3VGEYk5XdG4iLx1d9UGnGRieU9wxYlBhalPlI5GGzGwRHtJTPqQhdIbJQ0q36t6+Pxz9ZRyTEPmPH4YZ5qYzjn+Bkm6lt/ZnHhelNkBA9YBnY0RuxJb0UDcwnbBm/5soB8KmI2uboepljzS4b55cs
xMHVBNTPqdlTxalAz3bua5SsDPZBRD4gaGunPbAhpBmBiF6YS311D0CYk+iH5o8GeeE/duHTbEJkMq54e3kqmQ==
- AXVaBK5bVCC08qAd4XktZniE7Exu78WV64oLuIoAy0IMSUEWgVG/iLdNV+gTxCabX8TxfiL2QAenjAFIe0/ztA8ez7uVK6XgWvL5ZZGAhl17cUfUgvJ1t4ML8I0esy+DHhGqWJULF82q0xX0Qh9kHTlERz6l/QAyR2KxB/42MH2d6GLRvTmItb2qlWU7oA6mdkWK2T7JxmKd9Ku87FC0pdj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Uc0QBU4ZqOVUIqf9ncxIcCTG2OqC2aCSu4Adk3+a6sAZk1mbgM/5MPw==
g2e3jIQjF7giTc9mz9xRKGgd0KUVeRaqoAA7FHKdDR7e5D4iaGwpGfEr6plTtiSVXRvgE/XLHnjEXtIEMmUx4UQjQxgqmBPCHfspybLSZ1V1g0WtfZ3GS7I64+jMpNu0YMGLP7zpk7bg9H15EAQQDyaGdmdCHNoL+GoC9Uz1M3pl1R3gWL5k7i5pXqr3VrAvTJT6Lk7naEIPsh9J+wyEFA==
内置组合
+i3m0VjEPwL7DfLTg8mQ6EZujW2k82LP3IIevHy+vWn6LMBJa5HtBoC/Ew7baHjSAcaDhC1jwd6PXU4EOGgh1wL8cxGT12TuyXz2zjclc2vni6D/oRa/2FN+4NFRUbcCK72xaAKKDuEn812MdvcO0HL8/YuAE4eQe8xlQ6gWfe1RLtQ/lbaenXQFv6DEl4OM3U+ed8+23Q+GyMnffhxkeB/KPAANR9dfRQLM4K7LpbUAjT6s32qMMnfqTNGjc6+rbj0jPsiIO7wndvmrVT4GbuUVxqv50Ji/f2a88RMMX7Z7ch+kdk4x0wM7jiJ3JwTsrNjsgiSYJRhQLTJHkUHa5XoNQYNeT3BaYwrgn+JyasxR0QbJ1Ulxb1fEV1boAgIa
用户组合
FrCs1nu0Gp88jS8DvW7/j/VzBSQCw++W4smZwh8V01Hp3tbPHZ9B7Wvzc5V6zswdX2RgYeZnoVHFKEy1i/cosA==
export var SomeVar: { a: SomeType };
export interface SomeType {
count: number;
}
PCmYr5egbEsyPc0K/ZoLpVwf2kRky7TKv20zBpdLuRw=
import * as foo from "./foo";
let x: foo.SomeType = foo.SomeVar.a;
console.log(x.count);
GwV7iseDrkqpHzAMZcCMXA69yDTL1xo0pIU0moKrvWd359Hsa5cfVP0KGmCGCeUcKAaW0kkQj/nMSn53sDUdDAj2Cw51XDxQ8TWxSbc2drf8OgqL3IfFKHdeeUb7qSbnmWRP1EX/4RHVYXVd9whn2gcwE4gEUP/fU9oA2GRNyN/9WVNoMhLtK/oPZtHW1TsfOEOCJQMaMLUfX5MZYWbPES2W/1exX9OLkRRlY+V4cyCEZGPNcbQC+7UjeoKUGA7Ls1mnAdGFMX61gQ12P0bRbG4BU7Q2Me48qsrcCUB3w8wvyB62o5ACx5rykJAkiV1ppynNn1wmZCqPRMiA8R2HrqrWvcpDpXhYRhmySagJxDmX7sTLPnaoKa3ZjNPKumrN
export var Bar: { a: Bar };
export interface Bar {
count: number;
}
2NT/Zvq8txgLdMl6skRol/d+dfSwScs9TM1k+1itaWLrQLtc1DWJq+U7LG5mR2NaU/yh91c0q2UeuP/CP5A8TA==
import { Bar } from "./foo";
let x: Bar = Bar.a;
console.log(x.count);
+Bz7v0vKZuzOAg/DAllFzibiqfMZ5ypF3AWTkQdae9sIRiS6ZZjcmTf8JD79S8GjOCPYbQErqdzbBo77WCPKXryPvtdEGSNWaeUNtsuznztXctBk6d7wwqpp0tu8ltp+/7LwBkJ8jj2LwnZwHl+yZOweBs2RyBAgkYqqC6pGk6sUZccrCbFepFaQmAdRu9069n1wdGDbCPnVQ0XEIdncATK85RCmDJonZwLU+o3jffEM6WqUs3AleQiaZi+1coec
高级组合
vCAv4gVDumcSG7bukGlPkbvzQsiPXDRjek8BJBhhXPoHQy1MhsNcMPzUbYio5s3GlawLsfYhUe8P+059aQS+v7rq65r6VNfkVLaFkFVjnxeX5DeaBdLE5s4IRTCTryH82QkcLXqHEZlehVYSVi9zcXCeOjXR6ES3Lp5/KW93t6Lawopsq/oPW9kdPSczahaPiYoWRZCxw68DJKn+m2JozRJRoiYDNl/GUePLCKfapmPxN0/wuMm0Fd883Iv2G8E8
vIJ5xVy+9kEOatP14rXsFFpyHbYOdUPE3HCMGRipPpZnRi6mLNMIKdiszsi+RU3J0kY6SyrmJIRGIgjUr/vmFVmlgmrtkiswNHT6X3F9SeZTJQptAE22PsdUE6Y9jr9MifiSk2x7f/oUEBg95MJaAK3YZnfDxZKSpZarxb12TQAXmS01Q6i1ah1lgVjkJ/ZJ4v2hLqygmMxbM0UIxGu3xDWxg50Trb2NFzU9upoiL13NlxAKZi5beW4SVxZctW5cIUw/JwbSBdyTDH6OsvSUGhkWghgzde0yQ0r2ZjCAyxZGbR3izU+UyKY1jpuzvWrOFuZAbW3Y6X+4gX85n8q3suCkZ2IXoCwxJgXY14TTztgPt6tYtCL0MMXANO/AV8MaVNnescNp2wggyae6MhUAXA==
M6zd7qT9brh4+On++wOnPwmxnh/4d1REU2tB7Y76RRHf1YFrF5QXCwVLp3KCA0jH
使用 interface 添加
D8hMSmaW2zu8E5bzOEF36ktZ4eeBxygeLC60Q69px1pWiycHaHmvxXzIjGiW7STxDkSUwgWAcK2jPBixZqxQSaefKUCwfQg3m7mAc3rb+sCkn2xpGTfiat+3Dzl8VlWU5gvE2UXKefGZrTr9nCSrtg==
interface Foo {
x: number;
}
// ... elsewhere ...
interface Foo {
y: number;
}
let a: Foo = ...;
console.log(a.x + a.y); // OK
fyfU9fmXsgL5JciryrzjZ5YXCXOkkzawMYNBQVF4MFI=
class Foo {
x: number;
}
// ... elsewhere ...
interface Foo {
y: number;
}
let a: Foo = ...;
console.log(a.x + a.y); // OK
adLsUygHe9qJ4BUEWzOnC90yhOFG3MPTx9B1J2e3lP1KOXBBndkCoi1p/92Hu84OxN+wi0+iq3JwF+cgRyBCcyJISzEkQ3saZQT5NBzbeax4M/6zIUoSn8naPGcwprHO
使用 namespace 添加
twBc7qG4Qfyly7fdoPXe+8wW+UdLUIB8VsaVjf5E8zqezwewzcowV9iKwtFdWeI2c1PEpSw01QaFCuRAx9byXr0QSVB+HsndGGz/BPG0ms/iET1UfZF7usv0h66T4+exp/0zwGc71I/zKNetKCR9H9bpekrmQpMJAiSznRP+CwQ=
+tonNPsq9Zp6lMm5eLOphf1CnX2AY0+jMvecdopy40gQKHacXvcAOli2TeFMEgyv05iZariFZXb36mjz8O2B6Q==
class C {}
// ... elsewhere ...
namespace C {
export let x: number;
}
let y = C.x; // OK
FBB/kNaoO4/3OKLzD6j0wtXPCNIblgry1i1Ese8b1WVVAdzIMucGoWucff5vP5vh9b3+M8s0vDSPQmu9h+zA0zK6Ib8B9w97Ji2gMv2cIT8VmiloJuQ/WxaoWIRapqNP5kXA+jduCAJ3SjrzwIq52rIW1T/Ks0tIOTXAcjRO9Hk5/tnxZiy4yhN3V9igSQ3o6SFIEchLuo7s8oI7uUFoM382hC/O+mBi1O0oJ9dA2gZnH/zCPAfaE6qrmGQx5Xh0zMDAKOKNnv+9Qe+6JRyaZwdyuwUQUBydQEUvqJqb1EmkJGptUV0KGBiPhHVmV4Sy+jk+geiAVJ8tBwsSrrT9rlX7BsiaDtUEm5VU3+c8aek=
xD8n8iVvcmMP4e52P9nFzWEXQ655w2HKlZydg0QP/h+s/21eqCVjyX4UKFCAwNlQst8GDNueMw2lWysodT7E4Q==
class C {}
// ... elsewhere ...
namespace C {
export interface D {}
}
let y: C.D; // OK
VN76xCOUMI4UCv4cNvbJE2d1Ki9CVDHd5u+ogTCWjaBfjJvsJyjAzRMpJpBLVM+kA6oFBnz79sEaLiep/qzfJF06C4nWT7sMNsIPhbu1PIawVmycfVBkvsd35CQ6Io7ZDEl9eKg1xuVgQd5VwpMmD0uWlfi9cNlUvm0l4QEkvnR1CL97MnI6RxVYeJY9ido6Xh+egx5Ko62SG1m+HvZs1YA24cgZd32zNILNTUEB2VZW3RgAKE0GEKHCZKZ334rYBLfAPXg3DcBNN4XO0GUN7Ma2D2/zIGPGWLhi0IIwte0=
3Nq8p0tycApbKIP9NimyJtv8rfwoBZw+oRioBMYDlQYK3VFJ/Bljyb+W1qbWKwLdl80fx3bNs0Nt1sWCpPDYncjOx1wVsLXITfTEk4yNUxXGO83f5jtbCvufOpFyBgoq0Zvop4fmODLuz4jdPvjCvaYAVwo6ITB5cb3TekSyGKB4aYRUWNqmTjsIW9p7iqHglAA9pW0JRVvIL1IqmfIjwfNJ9Y5xW9b7JUlEM1IZ6Qg=
namespace X {
export interface Y {}
export class Z {}
}
// ... elsewhere ...
namespace X {
export var Y: number;
export namespace Z {
export class C {}
}
}
type X = string;
VN76xCOUMI4UCv4cNvbJEwIP5IhRKpNyNZ5mkQOCBJA6LXrbysdPLYcfaHNJj3XB87mOEzZm+eTnDbSZb1a8WA==
- J08lfoEmfM4AWYLdUeQ14apV2thuZF191PlgQV8xMiIDdF0g++cXQmOuMI0fKvkZhPbmozvUz9v+ufaiOSpT937gJJS+xrbwQkKmccPBzy0Lm8903+onlONuVyPgLalxEND4UpMOfX8pp1oxyaUbA7aC5vrJBvRTD0FlkRZwhuO+XXSBEv8zzUWni+xozpOcrvI40TbScXsfd/acN4OHWU8gSWBpU+fjIH5YKSHyVSaA+OBGCCkbOr+WD0TU7tBTrnNL1vq4KyXjiUS0PU94DDviF40WcNoCF7bXRkNTsHm+1vjchCBV6/1OOgs4ApS+3sFHW8Mn35dy/4IeqLGiF2EV+uNgNM9m57cZGSybnTHOrJYgkKMR0qcRVBVhPNNp5SBGLzOaVKZKw+V+sbfMTSv6Vo4Mm0TRGp3mOBkX1HBeGQi0MEq7Np7r0wzq0PQo4GZ3KgPpnbhNfZslOlmYADgHEFnaL6gz/pXWyuYXAAa1GpIh3K+49EobL9xgOdw88EtUGTnBGMZMzQwbn9difQEtt5530+z0k7DaZ+1qg1F7ctxnHnaEdTgB5Jty7TgfRLTyfbGw2/kKT2g89Q+bsg==
86f2Bm/dk4g95BowGnc/1mkdD+lmoWYScg+Leddx0QlJ8MXePnYP0ZTq3eMfM904
- 2iXrD8l1/ncDGt0SSntwdB0s48f13uMzA6AX33Sz27hXGHelXAAA026h7CcgmiGeWnRBtZz/42SVUUenIEIVKAnfD4nnDZDe0bJx/RnY+7ZCfvpPfh3dmsgJ0oP63Jspx5CfHVTyMj0BvJUOQMuJhKKAXpEYMV43lfE8yP/OO+cNNq9ff/eQ3NmsGqQ09PuV92mz1olIweEMB6hgRSFoP9LXYF/mkPSpWs57QUW0ZnY81WEZ6zTOaiVYoPaKiKZeufnONUj8ElnqRLe3xw1+1r2qq7DY0t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