{"id":4940,"date":"2023-01-05T11:23:15","date_gmt":"2023-01-05T03:23:15","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4940"},"modified":"2023-01-05T11:23:15","modified_gmt":"2023-01-05T03:23:15","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e9%a1%9e%e5%9e%8b%e6%96%b7%e8%a8%80typeassertion","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e7%9a%84%e9%a1%9e%e5%9e%8b%e6%96%b7%e8%a8%80typeassertion\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u7684\u985e\u578b\u65b7\u8a00(TypeAssertion)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u985e\u578b\u65b7\u8a00, Type Assertion\"><\/p>\n<h1>\u4ec0\u9ebc\u662f TypeScript &#8220;\u985e\u578b\u65b7\u8a00(Type Assertion)&#8221;<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u985e\u578b\u7cfb\u7d71\uff0c\u53ef\u4ee5\u5728\u7de8\u8b6f\u6642\u671f\u6aa2\u67e5\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002\u800c\u985e\u578b\u65b7\u8a00\uff08Type Assertion\uff09\u662f TypeScript \u4e2d\u7684\u4e00\u7a2e\u8a9e\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u8b6f\u6642\u671f\u6307\u5b9a\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002<\/p>\n<h2>\u985e\u578b\u65b7\u8a00\u7684\u8a9e\u6cd5<\/h2>\n<p>\u985e\u578b\u65b7\u8a00\u7684\u8a9e\u6cd5\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5728\u8b8a\u6578\u524d\u9762\u52a0\u4e0a <code>&lt;\u985e\u578b&gt;<\/code> \u5373\u53ef\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet str: any = 'Hello World';\nlet strLength: number = (<string>str).length;\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5c07 <code>str<\/code> \u8b8a\u6578\u7684\u985e\u578b\u65b7\u8a00\u70ba <code>string<\/code>\uff0c\u4ee5\u78ba\u4fdd <code>str<\/code> \u8b8a\u6578\u7684\u985e\u578b\u70ba <code>string<\/code>\uff0c\u800c\u4e0d\u662f <code>any<\/code>\u3002<\/p>\n<h2>\u985e\u578b\u65b7\u8a00\u7684\u512a\u9ede<\/h2>\n<p>\u985e\u578b\u65b7\u8a00\u7684\u6700\u5927\u512a\u9ede\u5728\u65bc\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u8b6f\u6642\u671f\u6307\u5b9a\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002\u6b64\u5916\uff0c\u985e\u578b\u65b7\u8a00\u4e5f\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u8b6f\u6642\u671f\u66f4\u52a0\u6e05\u695a\u5730\u77e5\u9053\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4ee5\u4fbf\u66f4\u52a0\u6709\u6548\u5730\u958b\u767c\u7a0b\u5f0f\u78bc\u3002<\/p>\n<h2>\u985e\u578b\u65b7\u8a00\u7684\u7f3a\u9ede<\/h2>\n<p>\u96d6\u7136\u985e\u578b\u65b7\u8a00\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u8b6f\u6642\u671f\u6307\u5b9a\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4f46\u662f\u5b83\u4e5f\u6709\u4e00\u4e9b\u7f3a\u9ede\u3002\u9996\u5148\uff0c\u985e\u578b\u65b7\u8a00\u53ef\u80fd\u6703\u5c0e\u81f4\u7a0b\u5f0f\u78bc\u8b8a\u5f97\u8907\u96dc\uff0c\u4e26\u4e14\u53ef\u80fd\u6703\u5c0e\u81f4\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u964d\u4f4e\u3002\u5176\u6b21\uff0c\u985e\u578b\u65b7\u8a00\u4e5f\u53ef\u80fd\u6703\u5c0e\u81f4\u7a0b\u5f0f\u78bc\u7684\u53ef\u7dad\u8b77\u6027\u964d\u4f4e\uff0c\u56e0\u70ba\u958b\u767c\u8005\u53ef\u80fd\u6703\u5fd8\u8a18\u6307\u5b9a\u8b8a\u6578\u7684\u985e\u578b\uff0c\u5c0e\u81f4\u7a0b\u5f0f\u78bc\u51fa\u73fe\u932f\u8aa4\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u7684\u985e\u578b\u65b7\u8a00\uff08Type Assertion\uff09\u662f\u4e00\u7a2e\u8a9e\u6cd5\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u8b6f\u6642\u671f\u6307\u5b9a\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u6b63\u78ba\u6027\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u8b6f\u6642\u671f\u66f4\u52a0\u6e05\u695a\u5730\u77e5\u9053\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4ee5\u4fbf\u66f4\u52a0\u6709\u6548\u5730\u958b\u767c\u7a0b\u5f0f\u78bc\u3002\u4f46\u662f\uff0c\u5b83\u4e5f\u6709\u4e00\u4e9b\u7f3a\u9ede\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u7a0b\u5f0f\u78bc\u8b8a\u5f97\u8907\u96dc\uff0c\u4e26\u4e14\u53ef\u80fd\u6703\u5c0e\u81f4\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u964d\u4f4e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:TypeScript\u4e2d\u7684\u985e\u578b\u65b7\u8a00(TypeAssertion)\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7a0b\u5f0f\u78bc\u4e2d\u6307\u5b9a\u8b8a\u6578\u7684\u985e\u578b\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u4e2d\u7684\u985e\u578b\u65b7\u8a00(TypeAssertion)\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-4940","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1hG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=4940"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4940\/revisions"}],"predecessor-version":[{"id":4941,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4940\/revisions\/4941"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}