{"id":1563,"date":"2022-09-15T18:07:38","date_gmt":"2022-09-15T10:07:38","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1563"},"modified":"2022-09-15T18:38:46","modified_gmt":"2022-09-15T10:38:46","slug":"typescript-zhong","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1563","title":{"rendered":"TypeScript \u4e2d !"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/09\/ts.jpeg\" alt=\"ts\" \/><\/p>\n<p>\u5728 JavaScript \u4e2d <code>!<\/code> \u4e00\u822c\u8868\u793a\u4e3a\u53d6\u53cd\u4e14\u8f6c\u6362\u4e3a <code>Boolean<\/code> \u503c\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">!0 \/\/ true\n!'' \/\/ true\n!-1 \/\/ false\n<\/code><\/pre>\n<p>\u5728 TypeScript \u4e2d\u5b83\u8fd8\u6709\u4e00\u4e2a\u540d\u5b57: <strong>Non-null assertion operator<\/strong>\uff0c\u5373\u4e3a\u975e\u7a7a\u5224\u65ad\u3002<\/p>\n<blockquote>\n<p>A new ! post-fix expression operator may be used to assert that its operand is non-null and non-undefined in contexts where the type checker is unable to conclude that fact.<\/p>\n<\/blockquote>\n<p>\u5c31\u662f\u8bf4\u7c7b\u578b\u68c0\u67e5\u5668\u4e0d\u80fd\u51c6\u786e\u68c0\u6d4b\u7c7b\u578b\u65f6\uff0c<code>!<\/code> \u5c31\u7528\u6765\u5f3a\u5236\u544a\u8bc9\u5b83\u8fd9\u91cc\u975e\u7a7a\uff08null\u3001undefined\uff09\u3002<\/p>\n<p>\u7b80\u5355\u6765\u8bf4\uff1a<strong>\u5c31\u662f\u628a\u64cd\u4f5c\u6570\u7684\u503c\u57df\u4e2d\u6392\u9664 null \u548c undefined<\/strong>\u3002<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<ol>\n<li>\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u4e86\u4e00\u4e2a\u63a5\u53e3\uff0c\u63a5\u53e3\u4e2d\u4e00\u4e2a\u5b57\u6bb5\u53ef\u9009\uff0c\u4f46\u662f\u7c7b\u4e2d\u7684\u90a3\u4e2a\u5b57\u6bb5\u80af\u5b9a\u5b58\u5728\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">interface B {\n    name?:string,\n}\n\nclass A implements B {\n    name!:string\n}\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u5f3a\u5236\u94fe\u5f0f\u8c03\u7528<\/li>\n<\/ol>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">myObject.name!.length\n<\/code><\/pre>\n<p>\u5c31\u662f\u544a\u8bc9\u7f16\u8bd1\u5668 name \u80af\u5b9a\u5b58\u5728\u3002<\/p>\n<ol start=\"3\">\n<li>\u4e0d\u591f\u806a\u660e\u7684\u7c7b\u578b\u68c0\u6d4b<\/li>\n<\/ol>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function isValid(x: any): boolean {\n  return x !== undefined &amp;&amp; x !== null;\n}\n\nfunction test(y?: string): string {\n  if (isValid(y)) {\n    return y.toLowerCase(); \/\/ \u5728 isValid \u5206\u652f\u5185\uff0cy \u660e\u660e\u4e0d\u53ef\u80fd\u662f undefined\uff0c\u4e3a\u4f55\u8fd8\u62a5\u9519\u300eObject is possibly 'undefined'.(2532)\u300f\uff1f\ud83e\udd14\n  }\n\n  return '';\n}\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/09\/16632379073253.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u56e0\u662f TypeScript \u4e0d\u80fd\u68c0\u6d4b\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u5982\u679c\u628a <code>isValid<\/code> \u6539\u4e3a\u5185\u8054\u7c7b\u578b\u5219\u53ef\u4ee5\u89e3\u51b3\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function test(y?:string):string{\n    if(y !== undefined &amp;&amp; y !== null){\n        return y.toLowerCase()\n    }\n    return ''\n}\n<\/code><\/pre>\n<p>\u82e5\u6211\u4eec\u7684\u5224\u65ad\u5f88\u590d\u6742\u6216\u8005\u60f3\u590d\u7528\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5c31\u4e0d\u662f\u5f88\u7406\u60f3\uff0c\u6b64\u65f6 <code>!<\/code> \u5c31\u53ef\u4ee5\u89e3\u51b3\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function test(y?: string): string {\n  if (isValid(y)) {\n    return y!.toLowerCase(); \/\/ y \u7ecf\u8fc7\u51fd\u6570\u6821\u9a8c\u540e\u4e0d\u53ef\u80fd\u662f undefined\uff0c\u6211\u4eec\u53ef\u901a\u8fc7\u975e\u7a7a\u65ad\u8a00\u544a\u77e5 TS\n  }\n\n  return '';\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 JavaScript \u4e2d ! \u4e00\u822c\u8868\u793a\u4e3a\u53d6\u53cd\u4e14\u8f6c\u6362\u4e3a Boolean \u503c\u3002 !0 \/\/ true&#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":[5,33,225],"class_list":["post-1563","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-css","tag-typescript","tag-225"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1563","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=1563"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1563\/revisions"}],"predecessor-version":[{"id":1564,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1563\/revisions\/1564"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}