{"id":1540,"date":"2022-09-10T20:12:21","date_gmt":"2022-09-10T12:12:21","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1540"},"modified":"2022-09-11T11:41:39","modified_gmt":"2022-09-11T03:41:39","slug":"typescript-unknown-vs-any","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1540","title":{"rendered":"TypeScript &#8211; unknown vs any"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/09\/16628119971939.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dmitripavlutin.com\/typescript-unknown-vs-any\/\" target=\"_blank\" rel=\"noopener\">https:\/\/dmitripavlutin.com\/typescript-unknown-vs-any\/<\/a><\/p>\n<p><code>any<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u53ef\u4ee5\u88ab\u8d4b\u503c\u4efb\u4f55\u503c\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let myVar:any = 0;\nmyVar = '1';\nmyVar = false;\n<\/code><\/pre>\n<p>\u8bb8\u591a TypeScript \u6307\u5357\u4e0d\u5efa\u8bae\u4f7f\u7528 <code>any<\/code> ,\u56e0\u4e3a\u5b83\u4f1a\u4e22\u6389\u7c7b\u578b\u9650\u5236 &#8212; \u8fd9\u6070\u662f\u4e3a\u4f55\u4f7f\u7528 TypeScript \u7684\u539f\u56e0\uff01<\/p>\n<p>TypeScript (3.0+ \u4ee5\u4e0a\u7248\u672c)\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7279\u6b8a\u7684\u7c7b\u578b <code>unknown<\/code>\uff0c\u7c7b\u4f3c\u4e8e <code>any<\/code>\u3002\u4f60\u540c\u6837\u4e5f\u53ef\u4ee5\u5411 <code>unkonwn<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u8d4b\u503c\u4efb\u4f55\u503c\uff1a<\/p>\n<blockquote>\n<p>TypeScript 3.0 introduces a new top type unknown. unknown is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn\u2019t assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.<\/p>\n<\/blockquote>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">let myVar:unknown = 0;\nmyVar = '1';\nmyVar = false;\n<\/code><\/pre>\n<p>\u73b0\u5728\u7684\u6700\u5927\u95ee\u9898\u662f\uff1a<code>any<\/code> \u548c <code>unknown<\/code> \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u8ba9\u6211\u4eec\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u627e\u5230\uff1a<\/p>\n<h3><a id=\"1-unknown-vs-any\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>1. unknown vs any<\/h3>\n<p>\u4e3a\u4e86\u66f4\u597d\u7684\u7406\u89e3 <code>unknown<\/code> \u4e0e <code>any<\/code> \u7684\u533a\u522b\uff0c\u8ba9\u6211\u4eec\u5199\u4e00\u4e2a\u51fd\u6570\u4ee5\u53ca\u5185\u90e8\u8c03\u7528\u5b83\u7684\u552f\u4e00\u53c2\u6570\u3002<\/p>\n<p>\u6211\u4eec\u4f7f <code>invokeAnything()<\/code> \u7684\u53c2\u6570\u4e3a <code>any<\/code> \u7c7b\u578b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function invokeAnything(callback: any) {\n  callback();\n}\n \ninvokeAnything(1);\n<\/code><\/pre>\n<p>\u56e0\u4e3a <code>callback<\/code> \u662f <code>any<\/code> \u7c7b\u578b\uff0c<code>callback()<\/code> \u8bed\u53e5\u4e0d\u4f1a\u89e6\u53d1\u7c7b\u578b\u9519\u8bef\uff0c\u4f60\u53ef\u4ee5\u5bf9 <code>any<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u505a\u4efb\u4f55\u4e8b\u3002<\/p>\n<p>\u4f46\u662f\u8fd0\u884c\u65f6\u811a\u672c\u4f1a\u629b\u51fa\u4e00\u4e2a\u8fd0\u884c\u65f6\u9519\u8bef\uff1a<code>TypeError: callback is not a function<\/code> \u3002<code>1<\/code> \u662f\u4e00\u4e2a\u6570\u5b57\u4e0d\u80fd\u88ab\u5f53\u505a\u51fd\u6570\u8c03\u7528 &#8212; \u540c\u65f6 TypeScript \u6ca1\u6709\u4fdd\u62a4\u4f60\u514d\u53d7\u6b64\u79cd\u9519\u8bef\u7684\u5f71\u54cd\uff01<\/p>\n<p>\u5982\u4f55\u5141\u8bb8 <code>invokeAnythings()<\/code> \u51fd\u6570\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u4f46\u662f\u5f3a\u5236\u5bf9\u53c2\u6570\u8fdb\u884c\u7c7b\u578b\u6821\u9a8c\uff0c\u4f8b\u5982\uff1a\u5982\u679c\u4ee5\u51fd\u6570\u6765\u8c03\u7528\u5b83\uff1f<\/p>\n<p>\u6b22\u8fce <code>unknown<\/code>!<\/p>\n<p><code>unknown<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u4e0e <code>any<\/code> \u7c7b\u578b\u7684\u7c7b\u4f3c\uff0c\u63a5\u53d7\u4efb\u610f\u503c\u3002\u4f46\u662f\u5f53\u5c1d\u8bd5\u4f7f\u7528 <code>unknown<\/code> \u7684\u53d8\u91cf\u65f6\uff0cTypeScript \u5f3a\u5236\u4e00\u4e2a\u7c7b\u578b\u6821\u9a8c\u3002\u4ee5\u6b64\u786e\u4fdd\u6b63\u662f\u4f60\u6240\u9700\u7684\u3002<\/p>\n<p>\u6211\u4eec\u6765\u628a <code>callback<\/code> \u53c2\u6570\u7684\u7c7b\u578b\u4ece <code>any<\/code> \u6539\u4e3a <code>unknown<\/code>\uff0c\u7136\u540e\u770b\u770b\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function invokeAnything(callback: unknown) {\n  callback();\nObject is of type 'unknown'.\n}\n \ninvokeAnything(1);\n<\/code><\/pre>\n<p>\u56e0\u4e3a <code>callback<\/code> \u53c2\u6570\u662f <code>unknown<\/code> \u7c7b\u578b\uff0c<code>callback()<\/code> \u8bed\u53e5\u6709\u4e00\u4e2a\u7c7b\u578b\u9519\u8bef <code>Object is of type 'unknown'<\/code>\u3002\u73b0\u5728\uff0c\u4e0e <code>any<\/code> \u76f8\u53cd\uff0c\u5f53\u8c03\u7528\u65f6 TypeScript \u4f7f\u4f60\u514d\u53d7\u53c2\u6570\u975e\u51fd\u6570\u7c7b\u578b\u7684\u9519\u8bef\u3002<\/p>\n<p>\u5728\u4f7f\u7528 <code>unknown<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u6821\u9a8c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f60\u9700\u8981\u7b80\u5355\u7684\u6821\u9a8c <code>callback<\/code> \u662f\u5426\u4e3a\u51fd\u6570\u7c7b\u578b\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function invokeAnything(callback: unknown) {\n  if (typeof callback === 'function') {\n    callback();\n       \n(parameter) callback: Function\n  }\n}\n \ninvokeAnything(1);\n<\/code><\/pre>\n<p>\u5df2\u7ecf\u6dfb\u52a0\u4e86 <code>typeof callback === 'function'<\/code> \u6821\u9a8c\uff0c\u4f60\u53ef\u4ee5\u5b89\u5168\u7684\u8c03\u7528 <code>callback()<\/code> \u56e0\u4e3a <code>unknown<\/code> \u5df2\u7ecf\u7f29\u5c0f\u4e3a <code>Function<\/code> \u7c7b\u578b\u3002\u6ca1\u6709\u7c7b\u578b\u9519\u8bef\u548c\u8fd0\u884c\u65f6\u9519\u8bef\uff01\u975e\u5e38\u68d2\uff01<\/p>\n<h3><a id=\"2-unknown%E4%B8%8E-any%E7%9A%84%E5%BF%83%E6%99%BA%E6%A8%A1%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>2. unknown \u4e0e any \u7684\u5fc3\u667a\u6a21\u578b<\/h3>\n<p>\u8bf4\u5b9e\u5728\u7684\uff0c\u5f53\u6211\u5f00\u59cb\u5b66\u4e60 <code>unknown<\/code> \u65f6\uff0c\u7406\u89e3\u8d77\u6765\u786e\u5b9e\u56f0\u96be\u3002\u7531\u4e8e\u5b83\u4e0e <code>any<\/code> \u90fd\u53ef\u4ee5\u63a5\u6536\u4efb\u4f55\u503c\uff0c\u90a3\u5230\u5e95\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n<p>\u4e0b\u9762\u662f\u5e2e\u52a9\u6211\u7406\u89e3\u5b83\u4eec\u4e0d\u540c\u7684\u51c6\u5219\uff1a<\/p>\n<ol>\n<li>\u4f60\u53ef\u4ee5\u7ed9 <code>unknown<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u8d4b\u503c\u4efb\u4f55\u503c\uff0c\u4f46\u662f\u5bf9\u5b83\u8fdb\u884c\u64cd\u4f5c\u4e4b\u524d\u5fc5\u987b\u7c7b\u578b\u68c0\u67e5\u6216\u7c7b\u578b\u65ad\u8a00\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u628a <code>unknown<\/code> \u60f3\u8c61\u4e3a <code>type unknown : number | string | boolean | ...<\/code>\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u7ed9 <code>any<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u8d4b\u503c\u4efb\u4f55\u503c\uff0c\u4ee5\u53ca\u7ed9\u5bf9\u5b83\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u5f88\u660e\u767d\u7684\u6f14\u793a\u4e86 <code>unknown<\/code> \u4e0e <code>any<\/code> \u4e4b\u95f4\u7684\u5f02\u540c\u3002<\/p>\n<p><code>unknown<\/code> \u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function invokeAnything(callback: unknown) {\n  if (typeof callback === 'function') {\n    callback();\n  }\n}\n \ninvokeAnything(1);\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684\u7c7b\u578b\u68c0\u67e5\u662f <code>typeof callback === 'function'<\/code> &#8212; \u68c0\u6d4b <code>callback<\/code> \u662f\u5426\u4e3a\u4e00\u4e2a\u51fd\u6570\u3002<code>callback<\/code> \u7684\u7c7b\u578b\u88ab\u9650\u5236\u4e3a\u51fd\u6570\u7c7b\u578b\u3002<\/p>\n<p><code>any<\/code> \u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function invokeAnything(callback: any) {\n  callback();\n}\n \ninvokeAnything(1);\n<\/code><\/pre>\n<p><code>callback<\/code> \u662f <code>any<\/code> \u7c7b\u578b\uff0cTypeScript \u4e0d\u4f1a\u5bf9 <code>callback()<\/code> \u8fdb\u884c\u4efb\u4f55\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<h3><a id=\"3%E6%80%BB%E7%BB%93\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>3. \u603b\u7ed3<\/h3>\n<p><code>unknown<\/code> \u4e0e <code>any<\/code> \u662f\u4e24\u4e2a\u7279\u6b8a\u7684\u53ef\u4ee5\u63a5\u53d7\u4efb\u4f55\u503c\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u56e0\u4e3a <code>unknown<\/code> \u63d0\u4f9b\u4e86\u7c7b\u578b\u5b89\u5168,\u6240\u4ee5\u76f8\u6bd4\u4e8e <code>any<\/code> \u66f4\u63a8\u8350\u5b83 &#8212; \u82e5\u4f60\u60f3\u5bf9 <code>unknown<\/code> \u8fdb\u884c\u64cd\u4f5c\u524d\u5fc5\u987b\u7c7b\u578b\u6821\u9a8c\u6216\u7f29\u5c0f\u5230\u7279\u5b9a\u7684\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dmitripavlutin.com\/typescript-unknown&#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":[150,33,219],"class_list":["post-1540","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-any","tag-typescript","tag-unknown"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1540","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=1540"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1540\/revisions"}],"predecessor-version":[{"id":1542,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1540\/revisions\/1542"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}