{"id":2035,"date":"2024-05-17T23:56:12","date_gmt":"2024-05-17T15:56:12","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=2035"},"modified":"2024-05-17T23:57:12","modified_gmt":"2024-05-17T15:57:12","slug":"typescript-declare","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=2035","title":{"rendered":"TypeScript &#8211; declare"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2024\/05\/1715961321.png\" alt=\"1715961321\" \/><\/p>\n<p>\u5728TypeScript\u4e2d\uff0c<code>declare<\/code>\u5173\u952e\u5b57\u4e3b\u8981\u7528\u4e8e\u58f0\u660e\u7c7b\u578b\u3001\u53d8\u91cf\u3001\u51fd\u6570\u3001\u6a21\u5757\u7b49\u7684\u5b58\u5728\uff0c\u4f46\u4e0d\u63d0\u4f9b\u5176\u5b9e\u73b0\u3002\u8fd9\u5bf9\u4e8e\u4e0eJavaScript\u5e93\u6216\u73b0\u6709\u4ee3\u7801\u96c6\u6210\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u544a\u8bc9TypeScript\u7f16\u8bd1\u5668\u8fd9\u4e9b\u5b9e\u4f53\u5df2\u7ecf\u5b58\u5728\uff0c\u5373\u4f7f\u5b83\u4eec\u5728\u4f60\u7684TypeScript\u6e90\u4ee3\u7801\u4e2d\u6ca1\u6709\u5b9e\u9645\u5b9a\u4e49\u3002\u8fd9\u6709\u52a9\u4e8eTypeScript\u66f4\u597d\u5730\u7406\u89e3\u548c\u9a8c\u8bc1\u4f60\u7684\u4ee3\u7801\uff0c\u540c\u65f6\u907f\u514d\u7c7b\u578b\u68c0\u67e5\u9519\u8bef\u3002\u4ee5\u4e0b\u662f<code>declare<\/code>\u7684\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\u548c\u6848\u4f8b\u3002<\/p>\n<h3><a id=\"%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u57fa\u672c\u8bed\u6cd5<\/h3>\n<ol>\n<li>\n<p><strong>\u58f0\u660e\u53d8\u91cf<\/strong>:<\/p>\n<pre><code class=\"language-typescript\">declare var variableName: type;\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u58f0\u660e\u51fd\u6570<\/strong>:<\/p>\n<pre><code class=\"language-typescript\">declare function functionName(param1: type1, param2: type2): returnType;\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u58f0\u660e\u6a21\u5757<\/strong>:<\/p>\n<pre><code class=\"language-typescript\">declare module moduleName {\n    export function funcName(param: type): returnType;\n    export var varName: type;\n    \/\/ ...\u5176\u4ed6\u58f0\u660e\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u58f0\u660e\u7c7b\u578b\u522b\u540d<\/strong>:<\/p>\n<pre><code class=\"language-typescript\">declare type typeName = type;\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5168\u5c40\u58f0\u660e<\/strong>:<\/p>\n<pre><code class=\"language-typescript\">declare global {\n    interface Window {\n        myCustomMethod: (arg: string) =&gt; void;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3><a id=\"%E4%BD%BF%E7%94%A8%E6%A1%88%E4%BE%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u6848\u4f8b<\/h3>\n<h4><a id=\"1%E5%A3%B0%E6%98%8E%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. \u58f0\u660e\u5168\u5c40\u53d8\u91cf<\/h4>\n<p>\u5047\u8bbe\u4f60\u6b63\u5728\u4f7f\u7528\u7684\u67d0\u4e2aJavaScript\u5e93\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u540d\u4e3a<code>myLib<\/code>\u7684\u5bf9\u8c61\uff0c\u4f46\u8fd9\u4e2a\u5bf9\u8c61\u5728\u4f60\u7684TypeScript\u4ee3\u7801\u4e2d\u6ca1\u6709\u5b9a\u4e49\u3002\u4f60\u53ef\u4ee5\u8fd9\u6837\u58f0\u660e\u5b83\uff1a<\/p>\n<pre><code class=\"language-typescript\">declare var myLib: any;\n<\/code><\/pre>\n<p>\u6216\u8005\uff0c\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u63d0\u4f9b\u66f4\u5177\u4f53\u7684\u7c7b\u578b\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-typescript\">declare var myLib: {\n    doSomething: () =&gt; void;\n    config: {\n        option1: string;\n    };\n};\n<\/code><\/pre>\n<h4><a id=\"2%E5%A3%B0%E6%98%8E%E5%A4%96%E9%83%A8%E6%A8%A1%E5%9D%97\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. \u58f0\u660e\u5916\u90e8\u6a21\u5757<\/h4>\n<p>\u5f53\u4f60\u4f7f\u7528\u672a\u5305\u542b\u7c7b\u578b\u5b9a\u4e49\u7684\u7b2c\u4e09\u65b9\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u58f0\u660e\u6a21\u5757\u6765\u63cf\u8ff0\u5176\u63a5\u53e3\uff1a<\/p>\n<pre><code class=\"language-typescript\">declare module 'myExternalLibrary' {\n    export function initialize(config: { apiKey: string }): void;\n    export class MyClass {\n        constructor(name: string);\n        someMethod(): number;\n    }\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u4f60\u5c31\u53ef\u4ee5\u50cf\u4f7f\u7528\u6709\u7c7b\u578b\u5b9a\u4e49\u7684\u6a21\u5757\u4e00\u6837\u4f7f\u7528\u8fd9\u4e2a\u5e93\u4e86\uff1a<\/p>\n<pre><code class=\"language-typescript\">import { initialize, MyClass } from 'myExternalLibrary';\n\ninitialize({ apiKey: 'myKey' });\nconst instance = new MyClass('instanceName');\n<\/code><\/pre>\n<h4><a id=\"3%E6%89%A9%E5%B1%95%E5%85%A8%E5%B1%80%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3. \u6269\u5c55\u5168\u5c40\u7c7b\u578b<\/h4>\n<p>\u5982\u679c\u4f60\u60f3\u5f80\u73b0\u6709\u7684\u5168\u5c40\u5bf9\u8c61\uff08\u5982<code>Window<\/code>\uff09\u4e0a\u6dfb\u52a0\u81ea\u5b9a\u4e49\u5c5e\u6027\u6216\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-typescript\">declare global {\n    interface Window {\n        myCustomMethod: (message: string) =&gt; void;\n    }\n}\n\nwindow.myCustomMethod = function(message) {\n    alert(message);\n};\n\n\/\/ \u73b0\u5728\u53ef\u4ee5\u5728TypeScript\u4e2d\u5b89\u5168\u5730\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\nwindow.myCustomMethod('Hello, world!');\n<\/code><\/pre>\n<p>\u901a\u8fc7<code>declare<\/code>\uff0cTypeScript\u80fd\u591f\u66f4\u597d\u5730\u4e0eJavaScript\u751f\u6001\u7cfb\u7edf\u4e2d\u7684\u5404\u79cd\u4ee3\u7801\u548c\u5e93\u534f\u540c\u5de5\u4f5c\uff0c\u540c\u65f6\u4fdd\u6301\u4e25\u683c\u7684\u7c7b\u578b\u68c0\u67e5\u548c\u4ee3\u7801\u63d0\u793a\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728TypeScript\u4e2d\uff0cdeclare\u5173\u952e\u5b57\u4e3b\u8981\u7528\u4e8e\u58f0\u660e\u7c7b\u578b\u3001\u53d8\u91cf\u3001\u51fd\u6570\u3001\u6a21\u5757\u7b49\u7684\u5b58\u5728\uff0c\u4f46\u4e0d\u63d0\u4f9b\u5176&#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],"tags":[33],"class_list":["post-2035","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","tag-typescript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2035","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=2035"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2035\/revisions"}],"predecessor-version":[{"id":2036,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/2035\/revisions\/2036"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}