{"id":2038,"date":"2024-05-18T23:21:42","date_gmt":"2024-05-18T15:21:42","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=2038"},"modified":"2024-05-18T23:26:56","modified_gmt":"2024-05-18T15:26:56","slug":"ts-declare-module-vs-declare-namespace","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=2038","title":{"rendered":"TS &#8211; declare module vs declare namespace"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2024\/05\/1716045749.png\" alt=\"1716045749\" \/><\/p>\n<p>\u5728TypeScript\u4e2d\uff0c<code>declare module<\/code> \u548c <code>declare namespace<\/code> \u90fd\u7528\u4e8e\u5b9a\u4e49\u7c7b\u578b\u4fe1\u606f\u4f9b\u7f16\u8bd1\u5668\u4f7f\u7528\uff0c\u4f46\u5b83\u4eec\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u5173\u952e\u5dee\u5f02\uff0c\u4e3b\u8981\u4f53\u73b0\u5728\u7ec4\u7ec7\u7ed3\u6784\u548c\u7528\u9014\u4e0a\uff1a<\/p>\n<h3><a id=\"declare-module\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>declare module<\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>: <code>declare module<\/code> \u4e3b\u8981\u7528\u4e8e\u63cf\u8ff0\u4e00\u4e2a\u5916\u90e8\u6a21\u5757\uff08\u901a\u5e38\u662f\u7b2c\u4e09\u65b9\u5e93\uff09\u7684\u7c7b\u578b\u4fe1\u606f\u3002\u5f53\u4f60\u4f7f\u7528\u7684JavaScript\u5e93\u6ca1\u6709\u81ea\u5e26\u7c7b\u578b\u5b9a\u4e49\u6587\u4ef6\uff08<code>.d.ts<\/code>\uff09\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u6765\u58f0\u660e\u8fd9\u4e2a\u6a21\u5757\u63d0\u4f9b\u7684\u63a5\u53e3\uff0c\u4ee5\u4fbf\u5728TypeScript\u9879\u76ee\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5e93\u800c\u4e0d\u4f1a\u5f15\u53d1\u7c7b\u578b\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7ec4\u7ec7\u7ed3\u6784<\/strong>: \u5b83\u5f3a\u8c03\u7684\u662f\u6a21\u5757\u7684\u8fb9\u754c\uff0c\u5141\u8bb8\u4f60\u4e3a\u6574\u4e2a\u6a21\u5757\u6216\u6a21\u5757\u5185\u7684\u7279\u5b9a\u90e8\u5206\uff08\u5982\u7c7b\u3001\u63a5\u53e3\u3001\u51fd\u6570\u7b49\uff09\u63d0\u4f9b\u7c7b\u578b\u58f0\u660e\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>:<\/p>\n<pre><code class=\"language-typescript\">declare module 'myModule' {\n    export function doSomething(): void;\n    export class MyClass {\n        constructor();\n        someMethod(): string;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3><a id=\"declare-namespace\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>declare namespace<\/h3>\n<ul>\n<li>\n<p><strong>\u7528\u9014<\/strong>: <code>declare namespace<\/code> \u5219\u66f4\u591a\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u903b\u8f91\u4e0a\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u7528\u6765\u7ec4\u7ec7\u76f8\u5173\u7684\u7c7b\u578b\u3001\u7c7b\u3001\u63a5\u53e3\u7b49\u3002\u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u5168\u5c40\u53d8\u91cf\u7684\u6c61\u67d3\uff0c\u5e76\u63d0\u4f9b\u4e00\u79cd\u5c42\u6b21\u5316\u7684\u8bbf\u95ee\u65b9\u5f0f\u3002\u5b83\u662fTypeScript\u4e2d\u7ec4\u7ec7\u4ee3\u7801\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u90a3\u4e9b\u6ca1\u6709\u91c7\u7528ES6\u6a21\u5757\u5316\uff08<code>import\/export<\/code>\uff09\u7684\u8001\u5f0fJavaScript\u4ee3\u7801\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7ec4\u7ec7\u7ed3\u6784<\/strong>: \u547d\u540d\u7a7a\u95f4\u53ef\u4ee5\u5d4c\u5957\uff0c\u5f62\u6210\u4e00\u79cd\u6811\u72b6\u7ed3\u6784\uff0c\u6709\u52a9\u4e8e\u7ba1\u7406\u5927\u578b\u9879\u76ee\u4e2d\u7684\u7c7b\u578b\u5b9a\u4e49\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u793a\u4f8b<\/strong>:<\/p>\n<pre><code class=\"language-typescript\">declare namespace MyLibrary {\n    export class Utility {\n        static formatText(text: string): string;\n    }\n\n    export namespace Network {\n        export function fetchData(url: string): Promise&lt;any&gt;;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3><a id=\"%E6%80%BB%E7%BB%93\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u603b\u7ed3<\/h3>\n<ul>\n<li>\u5f53\u4f60\u9700\u8981\u63cf\u8ff0\u4e00\u4e2a\u5916\u90e8\u6a21\u5757\u7684\u7c7b\u578b\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528 <code>declare module<\/code>\u3002<\/li>\n<li>\u5982\u679c\u4f60\u7684\u76ee\u6807\u662f\u7ec4\u7ec7\u5185\u90e8\u7684\u7c7b\u578b\u5b9a\u4e49\uff0c\u6216\u8005\u4e3a\u4e00\u4e2a\u8f83\u5927\u7684\u4ee3\u7801\u5e93\u521b\u5efa\u903b\u8f91\u5206\u7ec4\uff0c\u90a3\u4e48 <code>declare namespace<\/code> \u66f4\u4e3a\u5408\u9002\u3002<\/li>\n<\/ul>\n<p>\u968f\u7740\u73b0\u4ee3JavaScript\u548cTypeScript\u503e\u5411\u4e8e\u4f7f\u7528ES\u6a21\u5757\u7cfb\u7edf\uff0c<code>namespace<\/code>\u7684\u4f7f\u7528\u9010\u6e10\u51cf\u5c11\uff0c\u5c24\u5176\u662f\u5728\u65b0\u9879\u76ee\u4e2d\uff0c\u66f4\u591a\u7684\u63a8\u8350\u76f4\u63a5\u4f7f\u7528\u6a21\u5757\u5bfc\u5165\u5bfc\u51fa(<code>import\/export<\/code>)\u6765\u4ee3\u66ff\u547d\u540d\u7a7a\u95f4\u6765\u7ec4\u7ec7\u4ee3\u7801\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u6bd4\u5982\u7ef4\u62a4\u65e7\u4ee3\u7801\u5e93\u6216\u4e0e\u9057\u7559\u4ee3\u7801\u96c6\u6210\u65f6\uff0c<code>namespace<\/code>\u4ecd\u7136\u6709\u7528\u6b66\u4e4b\u5730\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728TypeScript\u4e2d\uff0cdeclare module \u548c declare namespace \u90fd\u7528&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[33],"class_list":["post-2038","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-typescript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2038"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2038\/revisions"}],"predecessor-version":[{"id":2039,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2038\/revisions\/2039"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}