{"id":182086,"date":"2020-10-30T22:47:19","date_gmt":"2020-10-30T14:47:19","guid":{"rendered":"http:\/\/4563.org\/?p=182086"},"modified":"2020-10-30T22:47:19","modified_gmt":"2020-10-30T14:47:19","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%8b-typescript-%e4%b8%8b-enum-%e7%9a%84-name-%e5%92%8c-value-%e7%9a%84%e9%97%ae%e9%a2%98%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=182086","title":{"rendered":"\u8bf7\u6559\u4e00\u4e0b typescript \u4e0b enum \u7684 name \u548c value \u7684\u95ee\u9898\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e00\u4e0b typescript \u4e0b enum \u7684 name \u548c value \u7684\u95ee\u9898\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>enum Month {       Jan='January',       Feb = 'February',       Mar='March'   }  console.log(Month.Feb);    for(var n in Month) {       console.log(n);     console.log(Month[n]); }    <\/code><\/pre>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre><code>[LOG]: \"February\"  [LOG]: \"Jan\"  [LOG]: \"January\"  [LOG]: \"Feb\"  [LOG]: \"February\"  [LOG]: \"Mar\"  [LOG]: \"March\"  <\/code><\/pre>\n<p>\u4f46\u662f\u7f16\u8bd1\u5668\u603b\u662f\u201c\u63d0\u793a\u201d\/\u201c\u62a5\u9519\u201d\uff1a<\/p>\n<pre><code>Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Month'. No index signature with a parameter of type 'string' was found on type 'typeof Month'. <\/code><\/pre>\n<p>string \u4e0d\u80fd\u7528\u4e8e enum \u8bbf\u95ee\u7684 index \u4e48\uff1f<\/p>\n<p>\u4f46\u662f\u7ed3\u679c\uff0c\u4f3c\u4e4e\u53c8\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002\u4e0d\u662f\u5f88\u7406\u89e3\u3002<\/p>\n<p>\u53c8\u6bd4\u5982\uff1a<\/p>\n<pre><code>const a : Month = Month[\"Jan\"]; console.log(a); console.log(typeof a); <\/code><\/pre>\n<pre><code>[LOG]: \"January\"  [LOG]: \"string\"  <\/code><\/pre>\n<p>\u6211\u5df2\u7ecf\u6307\u5b9a a \u7684 type \u662f Month\uff0c\u7f16\u8bd1\u5668\u6ca1\u6709\u62a5\u9519\uff0c\u4f46\u662f\u6253\u5370\u51fa\u6765\uff0ctypeof a \u662f string<\/p>\n<p>\u7ffb\u4e86\u534a\u5929 typescript \u7684\u6587\u6863\uff0c\u8c8c\u4f3c\u4e5f\u6ca1\u6709\u89e3\u91ca\u5f97\u5f88\u6e05\u695a\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3958132\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : noe132 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             enum \u4e0d\u5e94\u8be5\u5f53\u4f5c object \u6765\u7528\u3002<br \/>const enum \u5b9e\u9645\u4e0a\u6839\u672c\u4e0d\u5b58\u5728 runtine object \u3002<br \/>\u800c\u4e14 number enum \u6709 reverse mapping \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3958133\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Perry <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             TypeScript \u6700\u540e\u4f1a transpile \u6210 JavaScript\uff0c\u8fd9\u4e9b enum \u6700\u540e\u8fd8\u662f\u4f1a\u53d8\u6210\u4f60\u5b9a\u4e49\u7684 string                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3958134\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaojie668329 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Month \u662f\u4e2a\u7c7b\u578b\uff0c\u4f60\u628a\u5b83\u5f53\u503c\u6765\u7528\u4e0d\u5927\u5408\u9002\u5427\u3002\u867d\u7136 enum \u7684\u7279\u6027\u6700\u7ec8\u662f\u53ef\u4ee5\u904d\u5386\u7684\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3958135\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @noe132 \u7814\u7a76\u4e86\u4e00\u4e0b\uff0c\u786e\u5b9e\u3002\u76f4\u63a5\u4f7f\u7528 number enum \u66f4\u597d\u3002\u53cd\u6b63\u4ed6\u4e5f\u4f1a\u6dfb\u52a0 string \u7684\uff08\u901a\u8fc7 reverse maaping \uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3958136\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gouflv <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             enum \u6211\u4e00\u822c\u4f1a\u518d\u914d\u5408\u4e00\u4e2a \uff5b[enumKey]\uff1aname \uff5d\u7684\u5bf9\u8c61\u505a\u53d6\u503c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3958137\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gouflv \u4e0d\u597d\u610f\u601d\uff0c\u80fd\u591a\u8bb2\u4e00\u4e9b\u4e48\uff1f\u6ca1\u6709\u770b\u660e\u767d\u3002\u8c22\u8c22\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3958138\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ifsct <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540e\u534a\u90e8\u5206\uff0c\u4f60\u662f\u4e0d\u662f\u5f04\u6df7\u4e86 ts \u7684 typeof \u548c js \u7684 typeof                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e0b typescript \u4e0b&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/182086"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=182086"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/182086\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=182086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=182086"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=182086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}