{"id":622,"date":"2021-12-26T21:23:02","date_gmt":"2021-12-26T13:23:02","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=622"},"modified":"2021-12-26T21:32:00","modified_gmt":"2021-12-26T13:32:00","slug":"typescript-zhong-de-wen-hao-yu-gan-tan-hao-shi-s","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=622","title":{"rendered":"TypeScript\u4e2d\u7684\u95ee\u53f7 ? \u4e0e\u611f\u53f9\u53f7 ! \u662f\u4ec0\u4e48\u610f\u601d\uff1f"},"content":{"rendered":"<h2><a id=\"%E4%BB%80%E4%B9%88%E6%98%AF%EF%BC%9F%EF%BC%88%E9%97%AE%E5%8F%B7%EF%BC%89%E6%93%8D%E4%BD%9C%E7%AC%A6%EF%BC%9F\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4ec0\u4e48\u662f \uff1f\uff08\u95ee\u53f7\uff09\u64cd\u4f5c\u7b26\uff1f<\/h2>\n<p>\u5728TypeScript\u91cc\u9762\uff0c\u67093 4\u4e2a\u5730\u65b9\u4f1a\u51fa\u73b0\u95ee\u53f7\u64cd\u4f5c\u7b26\uff0c\u4ed6\u4eec\u5206\u522b\u662f<\/p>\n<p>\u4e09\u5143\u8fd0\u7b97\u7b26<\/p>\n<pre><code class=\"language-plain_text\">\/\/ \u5f53 isNumber(input) \u4e3a True \u662f\u8fd4\u56de ? : \u4e4b\u95f4\u7684\u90e8\u5206\uff1b isNumber(input) \u4e3a False \u65f6\n\/\/ \u8fd4\u56de : ; \u4e4b\u95f4\u7684\u90e8\u5206\nconst a = isNumber(input) ? input : String(input);\n<\/code><\/pre>\n<p>\u53c2\u6570<\/p>\n<pre><code class=\"language-plain_text\">\/\/ \u8fd9\u91cc\u7684 \uff1f\u8868\u793a\u8fd9\u4e2a\u53c2\u6570 field \u662f\u4e00\u4e2a\u53ef\u9009\u53c2\u6570\nfunction getUser(user: string, field?: string) {\n}\n<\/code><\/pre>\n<p>\u6210\u5458<\/p>\n<pre><code class=\"language-plain_text\">\/\/ \u8fd9\u91cc\u7684\uff1f\u8868\u793a\u8fd9\u4e2aname\u5c5e\u6027\u6709\u53ef\u80fd\u4e0d\u5b58\u5728\nclass A {\n  name?: string\n}\n\ninterface B {\n  name?: string\n}\n<\/code><\/pre>\n<p>\u5b89\u5168\u94fe\u5f0f\u8c03\u7528<\/p>\n<pre><code class=\"language-plain_text\">\/\/ \u8fd9\u91cc Error\u5bf9\u8c61\u5b9a\u4e49\u7684stack\u662f\u53ef\u9009\u53c2\u6570\uff0c\u5982\u679c\u8fd9\u6837\u5199\u7684\u8bdd\u7f16\u8bd1\u5668\u4f1a\u63d0\u793a\n\/\/ \u51fa\u9519 TS2532: Object is possibly 'undefined'.\nreturn new Error().stack.split('\\n');\n\n\/\/ \u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0?\u64cd\u4f5c\u7b26\uff0c\u5f53stack\u5c5e\u6027\u5b58\u5728\u65f6\uff0c\u8c03\u7528 stack.split\u3002\u82e5stack\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\nreturn new Error().stack?.split('\\n');\n\/\/ \u4ee5\u4e0a\u4ee3\u7801\u7b49\u540c\u4ee5\u4e0b\u4ee3\u7801, \u611f\u8c22 @dingyanhe \u7684\u76d1\u7763\n\nconst err = new Error();\nreturn err.stack &amp;&amp; err.stack.split('\\n');\n<\/code><\/pre>\n<h2><a id=\"%E4%BB%80%E4%B9%88%E6%98%AF%EF%BC%81%EF%BC%88%E6%84%9F%E5%8F%B9%E5%8F%B7%EF%BC%89%E6%93%8D%E4%BD%9C%E7%AC%A6\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4ec0\u4e48\u662f\uff01\uff08\u611f\u53f9\u53f7\uff09\u64cd\u4f5c\u7b26?<\/h2>\n<p>\u5728TypeScript\u91cc\u9762\u67093\u4e2a\u5730\u65b9\u4f1a\u51fa\u73b0\u611f\u53f9\u53f7\u64cd\u4f5c\u7b26\uff0c\u4ed6\u4eec\u5206\u522b\u662f<\/p>\n<p>\u4e00\u5143\u8fd0\u7b97\u7b26<\/p>\n<pre><code class=\"language-plain_text\">\/\/ ! \u5c31\u662f\u5c06\u4e4b\u540e\u7684\u7ed3\u679c\u53d6\u53cd\uff0c\u6bd4\u5982\uff1a\n\/\/ \u5f53 isNumber(input) \u4e3a True \u65f6\u8fd4\u56de False\uff1b isNumber(input) \u4e3a False \u65f6\u8fd4\u56deTrue\nconst a = !isNumber(input);\n\u6210\u5458\n\/\/ \u56e0\u4e3a\u63a5\u53e3B\u91cc\u9762name\u88ab\u5b9a\u4e49\u4e3a\u53ef\u7a7a\u7684\u503c\uff0c\u4f46\u662f\u5b9e\u9645\u60c5\u51b5\u662f\u4e0d\u4e3a\u7a7a\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\n\/\/ \u901a\u8fc7\u5728class\u91cc\u9762\u4f7f\u7528\uff01\uff0c\u91cd\u65b0\u5f3a\u8c03\u4e86name\u8fd9\u4e2a\u4e0d\u4e3a\u7a7a\u503c\nclass A implemented B {\n  name!: string\n}\n\ninterface B {\n  name?: string\n}\n<\/code><\/pre>\n<p>\u5f3a\u5236\u94fe\u5f0f\u8c03\u7528<\/p>\n<pre><code class=\"language-plain_text\">\/\/ \u8fd9\u91cc Error\u5bf9\u8c61\u5b9a\u4e49\u7684stack\u662f\u53ef\u9009\u53c2\u6570\uff0c\u5982\u679c\u8fd9\u6837\u5199\u7684\u8bdd\u7f16\u8bd1\u5668\u4f1a\u63d0\u793a\n\/\/ \u51fa\u9519 TS2532: Object is possibly 'undefined'.\nnew Error().stack.split('\\n');\n\n\/\/ \u6211\u4eec\u786e\u4fe1\u8fd9\u4e2a\u5b57\u6bb5100%\u51fa\u73b0\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u6dfb\u52a0\uff01\uff0c\u5f3a\u8c03\u8fd9\u4e2a\u5b57\u6bb5\u4e00\u5b9a\u5b58\u5728\nnew Error().stack!.split('\\n');\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f \uff1f\uff08\u95ee\u53f7\uff09\u64cd\u4f5c\u7b26\uff1f \u5728TypeScript\u91cc\u9762\uff0c\u67093 4\u4e2a\u5730\u65b9\u4f1a\u51fa\u73b0\u95ee\u53f7\u64cd\u4f5c\u7b26\uff0c\u4ed6\u4eec\u5206\u522b\u662f &#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":[20],"tags":[33],"class_list":["post-622","post","type-post","status-publish","format-standard","hentry","category-frontend","tag-typescript"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/622","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=622"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/622\/revisions"}],"predecessor-version":[{"id":623,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/622\/revisions\/623"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}