{"id":506,"date":"2025-12-05T14:57:47","date_gmt":"2025-12-05T06:57:47","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=506"},"modified":"2025-12-05T14:57:47","modified_gmt":"2025-12-05T06:57:47","slug":"typescript%e7%9a%84export%e7%94%a8%e6%b3%95%e7%a4%ba%e4%be%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/typescript%e7%9a%84export%e7%94%a8%e6%b3%95%e7%a4%ba%e4%be%8b%e8%af%a6%e8%a7%a3\/","title":{"rendered":"TypeScript\u7684export\u7528\u6cd5\u793a\u4f8b\u8be6\u89e3"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>TypeScript <code>export<\/code> \u7528\u6cd5\u8be6\u89e3<\/strong>\uff0c\u5305\u542b\u57fa\u7840\u5bfc\u51fa\u3001\u9ed8\u8ba4\u5bfc\u51fa\u3001\u547d\u540d\u5bfc\u51fa\u3001\u591a\u6a21\u5757\u5bfc\u51fa\u3001\u63a5\u53e3\/\u7c7b\u578b\u5bfc\u51fa\u4ee5\u53ca\u5b9e\u9645\u9879\u76ee\u793a\u4f8b\uff0c\u5e2e\u52a9\u4f60\u5168\u9762\u638c\u63e1 TypeScript \u6a21\u5757\u5316\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1\ufe0f\u20e3 \u57fa\u7840\u6982\u5ff5<\/h1>\n\n\n\n<p>\u5728 TypeScript\uff08\u548c ES6\uff09\u4e2d\uff0c<code>export<\/code> \u7528\u4e8e <strong>\u5c06\u6a21\u5757\u4e2d\u7684\u53d8\u91cf\u3001\u51fd\u6570\u3001\u7c7b\u3001\u63a5\u53e3\u3001\u7c7b\u578b\u7b49\u5bfc\u51fa<\/strong>\uff0c\u65b9\u4fbf\u5728\u5176\u4ed6\u6a21\u5757\u4e2d <code>import<\/code> \u4f7f\u7528\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>TypeScript \u6a21\u5757\u5316\u4e0e ES6 \u5b8c\u5168\u517c\u5bb9\uff0c\u4f46\u589e\u52a0\u4e86 <strong>\u63a5\u53e3\u548c\u7c7b\u578b\u7684\u5bfc\u51fa<\/strong>\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2\ufe0f\u20e3 \u547d\u540d\u5bfc\u51fa\uff08Named Export\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ math.ts\nexport const PI = 3.14159;\n\nexport function add(a: number, b: number): number {\n  return a + b;\n}\n\nexport class Circle {\n  constructor(public radius: number) {}\n  area(): number {\n    return PI * this.radius ** 2;\n  }\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5bfc\u5165\u65b9\u5f0f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ app.ts\nimport { PI, add, Circle } from &#039;.\/math&#039;;\n\nconsole.log(PI);\nconsole.log(add(2, 3));\n\nconst c = new Circle(5);\nconsole.log(c.area());\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u5bfc\u51fa\u591a\u4e2a\u6210\u5458<\/li>\n\n\n\n<li>\u5bfc\u5165\u65f6\u5fc5\u987b\u4f7f\u7528\u76f8\u540c\u540d\u5b57\uff08\u53ef\u7528 <code>as<\/code> \u91cd\u547d\u540d\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">3\ufe0f\u20e3 \u9ed8\u8ba4\u5bfc\u51fa\uff08Default Export\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ logger.ts\nexport default function log(message: string) {\n  console.log(message);\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5bfc\u5165\u65b9\u5f0f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport log from &#039;.\/logger&#039;;\n\nlog(&#039;Hello TypeScript!&#039;);\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u4e2a\u6a21\u5757\u53ea\u80fd\u6709\u4e00\u4e2a\u9ed8\u8ba4\u5bfc\u51fa<\/li>\n\n\n\n<li>\u5bfc\u5165\u65f6\u53ef\u4ee5\u968f\u610f\u547d\u540d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">4\ufe0f\u20e3 \u6df7\u5408\u5bfc\u51fa\uff08Named + Default\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ utils.ts\nexport const sum = (a: number, b: number) =&gt; a + b;\nexport const multiply = (a: number, b: number) =&gt; a * b;\n\nexport default function log(msg: string) {\n  console.log(msg);\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5bfc\u5165\u65b9\u5f0f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport log, { sum, multiply } from &#039;.\/utils&#039;;\n\nlog(&#039;Logging...&#039;);\nconsole.log(sum(2,3));\nconsole.log(multiply(2,3));\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">5\ufe0f\u20e3 \u5bfc\u51fa\u63a5\u53e3\u548c\u7c7b\u578b<\/h1>\n\n\n\n<p>TypeScript \u7279\u6709\uff0c\u53ef\u4ee5\u5bfc\u51fa\u63a5\u53e3\uff08interface\uff09\u3001\u7c7b\u578b\u522b\u540d\uff08type\uff09\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ types.ts\nexport interface User {\n  id: number;\n  name: string;\n}\n\nexport type ID = number | string;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5bfc\u5165\u65b9\u5f0f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport { User, ID } from &#039;.\/types&#039;;\n\nconst user: User = { id: 1, name: &#039;Alice&#039; };\nconst uid: ID = &#039;abc123&#039;;\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u63a5\u53e3\u548c\u7c7b\u578b\u53ea\u5728\u7f16\u8bd1\u9636\u6bb5\u5b58\u5728<\/li>\n\n\n\n<li>\u4e0d\u4f1a\u751f\u6210\u8fd0\u884c\u65f6\u4ee3\u7801<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">6\ufe0f\u20e3 \u91cd\u65b0\u5bfc\u51fa\uff08Re-export\uff09<\/h1>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u7edf\u4e00\u5bfc\u51fa\u591a\u4e2a\u6a21\u5757\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\/\/ index.ts\nexport * from &#039;.\/math&#039;;\nexport { default as log } from &#039;.\/logger&#039;;\nexport * from &#039;.\/types&#039;;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5bfc\u5165\u65b9\u5f0f<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport { PI, add, User } from &#039;.\/index&#039;;\nimport { log } from &#039;.\/index&#039;;\n\nlog(&#039;\u7edf\u4e00\u5bfc\u51fa\u6d4b\u8bd5&#039;);\n\n<\/pre><\/div>\n\n\n<p>\u2705 \u4f18\u52bf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u521b\u5efa\u6a21\u5757 <strong>\u5165\u53e3\u6587\u4ef6<\/strong>\uff08barrel file\uff09<\/li>\n\n\n\n<li>\u7b80\u5316\u5bfc\u5165\u8def\u5f84<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">7\ufe0f\u20e3 \u5176\u4ed6\u9ad8\u7ea7\u7528\u6cd5<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">7.1 \u91cd\u547d\u540d\u5bfc\u51fa<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport { add as addition, multiply as mul } from &#039;.\/utils&#039;;\n\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bfc\u5165\u65f6\u4f7f\u7528\u91cd\u547d\u540d\u540e\u7684\u540d\u5b57\uff1a<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport { addition, mul } from &#039;.\/utils&#039;;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">7.2 \u5bfc\u51fa\u65f6\u5b9a\u4e49\u7c7b\u578b<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport type Callback = (result: string) =&gt; void;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">7.3 \u5bfc\u51fa\u5e38\u91cf\u679a\u4e3e<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport enum Status {\n  SUCCESS,\n  FAIL,\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">8\ufe0f\u20e3 \u5c0f\u7ed3<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5bfc\u51fa\u65b9\u5f0f<\/th><th>\u793a\u4f8b<\/th><th>\u7279\u70b9<\/th><\/tr><\/thead><tbody><tr><td>\u547d\u540d\u5bfc\u51fa<\/td><td><code>export const a = 1<\/code><\/td><td>\u53ef\u5bfc\u51fa\u591a\u4e2a\uff0c\u5bfc\u5165\u9700\u4e00\u81f4<\/td><\/tr><tr><td>\u9ed8\u8ba4\u5bfc\u51fa<\/td><td><code>export default function(){}<\/code><\/td><td>\u6bcf\u4e2a\u6a21\u5757\u4e00\u4e2a\uff0c\u5bfc\u5165\u53ef\u4efb\u610f\u547d\u540d<\/td><\/tr><tr><td>\u63a5\u53e3\/\u7c7b\u578b\u5bfc\u51fa<\/td><td><code>export interface User {}<\/code><\/td><td>\u7f16\u8bd1\u65f6\u5b58\u5728\uff0c\u4e0d\u751f\u6210\u8fd0\u884c\u65f6\u4ee3\u7801<\/td><\/tr><tr><td>\u91cd\u65b0\u5bfc\u51fa<\/td><td><code>export * from '.\/mod'<\/code><\/td><td>\u521b\u5efa\u7edf\u4e00\u5165\u53e3\uff0c\u7b80\u5316\u5bfc\u5165<\/td><\/tr><tr><td>\u91cd\u547d\u540d\u5bfc\u51fa<\/td><td><code>export { a as b }<\/code><\/td><td>\u5bfc\u5165\u65f6\u4f7f\u7528\u65b0\u540d\u5b57<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd TypeScript export \u7528\u6cd5\u8be6\u89e3\uff0c\u5305\u542b\u57fa\u7840\u5bfc\u51fa\u3001\u9ed8&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/typescript%e7%9a%84export%e7%94%a8%e6%b3%95%e7%a4%ba%e4%be%8b%e8%af%a6%e8%a7%a3\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-506","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=506"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/506\/revisions"}],"predecessor-version":[{"id":507,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/506\/revisions\/507"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}