{"id":229493,"date":"2021-11-12T10:19:24","date_gmt":"2021-11-12T02:19:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=229493"},"modified":"2021-10-31T15:20:57","modified_gmt":"2021-10-31T07:20:57","slug":"linux-typescript-java","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-typescript-java.html","title":{"rendered":"\u7b80\u5355\u63a2\u8ba8TypeScript \u679a\u4e3e\u7c7b\u578b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86TypeScript \u679a\u4e3e\u7c7b\u578b\uff0cTypeScript \u5728 ES \u539f\u6709\u7c7b\u578b\u57fa\u7840\u4e0a\u52a0\u5165\u679a\u4e3e\u7c7b\u578b\uff0c\u4f7f\u5f97\u5728 TypeScript \u4e2d\u4e5f\u53ef\u4ee5\u7ed9\u4e00\u7ec4\u6570\u503c\u8d4b\u4e88\u540d\u5b57\uff0c\u8fd9\u6837\u5bf9\u5f00\u53d1\u8005\u6bd4\u8f83\u53cb\u597d\uff0c\u53ef\u4ee5\u7406\u89e3\u679a\u4e3e\u5c31\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u6587\u7ae0\u5185\u5bb9\u56f4\u7ed5TypeScript \u679a\u4e3e\u7c7b\u578b\u5c55\u5f00\u5185\u5bb9,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

TypeScript \u5728 ES \u539f\u6709\u7c7b\u578b\u57fa\u7840\u4e0a\u52a0\u5165\u679a\u4e3e\u7c7b\u578b\uff0c\u4f7f\u5f97\u5728 TypeScript \u4e2d\u4e5f\u53ef\u4ee5\u7ed9\u4e00\u7ec4\u6570\u503c\u8d4b\u4e88\u540d\u5b57\uff0c\u8fd9\u6837\u5bf9\u5f00\u53d1\u8005\u6bd4\u8f83\u53cb\u597d\uff0c\u53ef\u4ee5\u7406\u89e3\u679a\u4e3e\u5c31\u662f\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n

\u679a\u4e3e\u7c7b\u578b\u4f7f\u7528enum\u6765\u5b9a\u4e49\uff1a<\/p>\n

\r\nenum Day {\r\n  SUNDAY,\r\n  MONDAY,\r\n  TUESDAY,\r\n  WEDNESDAY,\r\n  THURSDAY,\r\n  FRIDAY,\r\n  SATURDAY\r\n }<\/pre>\n

\u4e0a\u9762\u5b9a\u4e49\u7684\u679a\u4e3e\u7c7b\u578b\u7684Day\uff0c\u5b83\u67097\u4e2a\u503c\uff0cTypeScript\u4f1a\u4e3a\u5b83\u4eec\u6bcf\u4e2a\u503c\u5206\u914d\u7f16\u53f7\uff0c\u9ed8\u8ba4\u4ece0\u5f00\u59cb\uff0c\u5728\u4f7f\u7528\u65f6\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u540d\u5b57\u800c\u4e0d\u9700\u8981\u8bb0\u6570\u5b57\u548c\u540d\u79f0\u7684\u5bf9\u5e94\u5173\u7cfb\u4e86\uff1a<\/p>\n

\r\nenum Day {\r\n  SUNDAY = 0,\r\n  MONDAY = 1,\r\n  TUESDAY = 2,\r\n  WEDNESDAY = 3,\r\n  THURSDAY = 4,\r\n  FRIDAY = 5,\r\n  SATURDAY = 6\r\n}<\/pre>\n

\u4e0b\u9762\u662f\u5c06\u4e0a\u9762\u4ee3\u7801\u8f6c\u8bd1\u4e3a JavaScript \u540e\u7684\u6548\u679c\uff1a<\/p>\n

\r\nvar Day = void 0;\r\n(function (Day) {\r\n  Day[Day[\"SUNDAY\"] = 0] = \"SUNDAY\";\r\n  Day[Day[\"MONDAY\"] = 1] = \"MONDAY\";\r\n  Day[Day[\"TUESDAY\"] = 2] = \"TUESDAY\";\r\n  Day[Day[\"WEDNESDAY\"] = 3] = \"WEDNESDAY\";\r\n  Day[Day[\"THURSDAY\"] = 4] = \"THURSDAY\";\r\n  Day[Day[\"FRIDAY\"] = 5] = \"FRIDAY\";\r\n  Day[Day[\"SATURDAY\"] = 6] = \"SATURDAY\";\r\n})(Day || (Day = {}));<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u6bcf\u4e00\u4e2a\u503c\u90fd\u88ab\u8d4b\u4e88\u4e86\u5bf9\u5e94\u7684\u6570\u5b57\u3002<\/p>\n

\u5728TypeScript\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u70b9\u7684\u5f62\u5f0f\u83b7\u53d6\u679a\u4e3e\u96c6\u5408\u4e2d\u7684\u6210\u5458\uff1a<\/p>\n

\r\nconsole.log(Day.SUNDAY)   \/\/ 0\r\nconsole.log(Day.MONDAY)   \/\/ 1<\/pre>\n

\u8bf4\u5b8c\u679a\u4e3e\u7c7b\u578b\u7684\u57fa\u672c\u4f7f\u7528\uff0c\u4e0b\u9762\u5c31\u6765\u770b\u4e00\u4e0b\u5e38\u89c1\u7684\u679a\u4e3e\u7c7b\u578b\u3002<\/p>\n

1. \u6570\u5b57\u679a\u4e3e<\/strong><\/div>\n

\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5728\u4ec5\u6307\u5b9a\u5e38\u91cf\u547d\u540d\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9a\u4e49\u7684\u5c31\u662f\u4e00\u4e2a\u9ed8\u8ba4\u4ece 0 \u5f00\u59cb\u9012\u589e\u7684\u6570\u5b57\u96c6\u5408\uff0c\u79f0\u4e4b\u4e3a\u6570\u5b57\u679a\u4e3e\u3002\u5982\u679c\u60f3\u8981\u4ece\u5176\u4ed6\u503c\u5f00\u59cb\u9012\u589e\uff0c\u53ef\u4ee5\u5c06\u7b2c\u4e00\u4e2a\u503c\u7684\u7d22\u5f15\u503c\u8fdb\u884c\u6307\u5b9a\uff1a<\/p>\n

\r\nenum Color {\r\n  Red = 2,\r\n  Blue,\r\n  Yellow\r\n}\r\nconsole.log(Color.Red, Color.Blue, Color.Yellow); \/\/ 2 3 4<\/pre>\n

\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u5b57\u6bb5\u6307\u5b9a\u4e00\u4e2a\u7d22\u5f15\u503c\uff0c\u90a3\u4ed6\u540e\u9762\u6ca1\u6709\u6307\u5b9a\u7d22\u5f15\u503c\u7684\u5c31\u4f1a\u4f9d\u6b21\u52a0\u4e00\uff1a<\/p>\n

\r\n\/\/ \u6307\u5b9a\u90e8\u5206\u5b57\u6bb5\uff0c\u5176\u4ed6\u4f7f\u7528\u9ed8\u8ba4\u9012\u589e\u7d22\u5f15\r\nenum Status {\r\n  Ok = 200,\r\n  Created,\r\n  Accepted,\r\n  BadRequest = 400,\r\n  Unauthorized\r\n}\r\nconsole.log(Status.Created, Status.Accepted, Status.Unauthorized); \/\/ 201 202 401<\/pre>\n

\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed9\u6bcf\u4e2a\u5b57\u6bb5\u6307\u5b9a\u4e0d\u8fde\u7eed\u7684\u4efb\u610f\u7d22\u5f15\u503c\uff1a<\/p>\n

\r\nenum Status {\r\n  Success = 200,\r\n  NotFound = 404,\r\n  Error = 500\r\n}\r\nconsole.log(Status.Success, Status.NotFound, Status.Error); \/\/ 200 404 500<\/pre>\n

\u6570\u5b57\u679a\u4e3e\u5728\u5b9a\u4e49\u503c\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8ba1\u7b97\u503c\u548c\u5e38\u91cf\u3002\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u5982\u679c\u67d0\u4e2a\u5b57\u6bb5\u4f7f\u7528\u4e86\u8ba1\u7b97\u503c\u6216\u5e38\u91cf\uff0c\u90a3\u4e48\u8be5\u5b57\u6bb5\u540e\u9762\u7d27\u63a5\u7740\u7684\u5b57\u6bb5\u5fc5\u987b\u8bbe\u7f6e\u521d\u59cb\u503c\uff0c\u8fd9\u91cc\u4e0d\u80fd\u4f7f\u7528\u9ed8\u8ba4\u7684\u9012\u589e\u503c\u4e86\uff0c\u6765\u770b\u4f8b\u5b50\uff1a<\/p>\n

\r\n\/\/ \u521d\u503c\u4e3a\u8ba1\u7b97\u503c\r\nconst getValue = () => {\r\n  return 0;\r\n};\r\nenum ErrorIndex {\r\n  a = getValue(),\r\n  b, \/\/ error \u679a\u4e3e\u6210\u5458\u5fc5\u987b\u5177\u6709\u521d\u59cb\u5316\u7684\u503c\r\n  c\r\n}\r\nenum RightIndex {\r\n  a = getValue(),\r\n  b = 1,\r\n  c\r\n}\r\n\/\/ \u521d\u503c\u4e3a\u5e38\u91cf\r\nconst Start = 1;\r\nenum Index {\r\n  a = Start,\r\n  b, \/\/ error \u679a\u4e3e\u6210\u5458\u5fc5\u987b\u5177\u6709\u521d\u59cb\u5316\u7684\u503c\r\n  c\r\n}\r\n<\/pre>\n
2. \u5b57\u7b26\u4e32\u679a\u4e3e<\/strong><\/div>\n

TypeScript \u5c06\u5b9a\u4e49\u503c\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u679a\u4e3e\u79f0\u4e3a\u5b57\u7b26\u4e32\u679a\u4e3e\uff0c\u5b57\u7b26\u4e32\u679a\u4e3e\u503c\u8981\u6c42\u6bcf\u4e2a\u5b57\u6bb5\u7684\u503c\u90fd\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u6216\u8005\u662f\u8be5\u679a\u4e3e\u503c\u4e2d\u53e6\u4e00\u4e2a\u5b57\u7b26\u4e32\u679a\u4e3e\u6210\u5458\uff1a<\/p>\n

\r\n\/\/ \u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\r\nenum Message {\r\n  Error = \"Sorry, error\",\r\n  Success = \"Hoho, success\"\r\n}\r\nconsole.log(Message.Error); \/\/ 'Sorry, error'\r\n \r\n\/\/ \u4f7f\u7528\u679a\u4e3e\u503c\u4e2d\u5176\u4ed6\u679a\u4e3e\u6210\u5458\r\nenum Message {\r\n  Error = \"error message\",\r\n  ServerError = Error,\r\n  ClientError = Error\r\n}\r\nconsole.log(Message.Error); \/\/ 'error message'\r\nconsole.log(Message.ServerError); \/\/ 'error message'<\/pre>\n

\u6ce8\u610f\uff1a\u8fd9\u91cc\u7684\u5176\u4ed6\u679a\u4e3e\u6210\u5458\u6307\u7684\u662f\u540c\u4e00\u4e2a\u679a\u4e3e\u503c\u4e2d\u7684\u679a\u4e3e\u6210\u5458\uff0c\u56e0\u4e3a\u5b57\u7b26\u4e32\u679a\u4e3e\u4e0d\u80fd\u4f7f\u7528\u5e38\u91cf\u6216\u8005\u8ba1\u7b97\u503c\uff0c\u6240\u4ee5\u4e0d\u80fd\u4f7f\u7528\u5176\u4ed6\u679a\u4e3e\u503c\u4e2d\u7684\u6210\u5458\u3002<\/p>\n

3. \u53cd\u5411\u6620\u5c04<\/strong><\/div>\n

\u5b9a\u4e49\u679a\u4e3e\u7c7b\u578b\u7684\u503c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 Enum['key'] \u6216\u8005 Enum.key \u7684\u5f62\u5f0f\u83b7\u53d6\u5230\u5bf9\u5e94\u7684\u503c value\u3002TypeScript \u8fd8\u652f\u6301\u53cd\u5411\u6620\u5c04\uff0c\u4f46\u662f\u53cd\u5411\u6620\u5c04\u53ea\u652f\u6301\u6570\u5b57\u679a\u4e3e\uff0c\u4e0d\u652f\u6301\u5b57\u7b26\u4e32\u679a\u4e3e\u3002<\/p>\n

\u6765\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n

\r\nenum Status {\r\n  Success = 200,\r\n  NotFound = 404,\r\n  Error = 500\r\n}\r\nconsole.log(Status[\"Success\"]); \/\/ 200\r\nconsole.log(Status[200]); \/\/ 'Success'\r\nconsole.log(Status[Status[\"Success\"]]); \/\/ 'Success'<\/pre>\n

TypeScript \u4e2d\u5b9a\u4e49\u7684\u679a\u4e3e\uff0c\u7f16\u8bd1\u4e4b\u540e\u5176\u5b9e\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u751f\u6210\u7684\u4ee3\u7801\u4e2d\uff0c\u679a\u4e3e\u7c7b\u578b\u88ab\u7f16\u8bd1\u6210\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u5305\u542b\u4e86\u6b63\u5411\u6620\u5c04\uff08 name -> value\uff09\u548c\u53cd\u5411\u6620\u5c04\uff08 value -> name\uff09\u3002<\/p>\n

\u4e0b\u9762\u6765\u770b\u770b\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684 Status \u7f16\u8bd1\u540e\u7684\u6548\u679c\uff1a<\/p>\n

\r\n{\r\n    200: \"Success\",\r\n    404: \"NotFound\",\r\n    500: \"Error\",\r\n    Error: 500,\r\n    NotFound: 404,\r\n    Success: 200\r\n}<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0cTypeScript \u4f1a\u628a\u5b9a\u4e49\u7684\u679a\u4e3e\u503c\u7684\u5b57\u6bb5\u540d\u5206\u522b\u4f5c\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\u548c\u5c5e\u6027\u503c\uff0c\u628a\u679a\u4e3e\u503c\u7684\u5b57\u6bb5\u503c\u5206\u522b\u4f5c\u4e3a\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u548c\u5c5e\u6027\u540d\uff0c\u540c\u65f6\u6dfb\u52a0\u5230\u5bf9\u8c61\u4e2d\u3002\u8fd9\u6837\u65e2\u53ef\u4ee5\u901a\u8fc7\u679a\u4e3e\u503c\u7684\u5b57\u6bb5\u540d\u5f97\u5230\u503c\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u679a\u4e3e\u503c\u7684\u503c\u5f97\u5230\u5b57\u6bb5\u540d\u3002<\/p>\n

4. \u5f02\u6784\u679a\u4e3e<\/strong><\/div>\n

\u5f02\u6784\u679a\u4e3e\u5c31\u662f\u679a\u4e3e\u503c\u4e2d\u6210\u5458\u503c\u65e2\u6709\u6570\u5b57\u7c7b\u578b\u53c8\u6709\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u5982\u4e0b\uff1a<\/p>\n

\r\nenum Result {\r\n  Faild = 0,\r\n  Success = \"Success\"\r\n}<\/pre>\n

\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u5efa\u8bae\u4f7f\u7528\u5f02\u6b65\u679a\u4e3e\u3002\u56e0\u4e3a\u5f80\u5f80\u5c06\u4e00\u7c7b\u503c\u6574\u7406\u4e3a\u4e00\u4e2a\u679a\u4e3e\u503c\u65f6\uff0c\u5b83\u4eec\u7684\u7279\u70b9\u662f\u76f8\u4f3c\u7684\u3002\u6bd4\u5982\u5728\u505a\u63a5\u53e3\u8bf7\u6c42\u65f6\u7684\u8fd4\u56de\u72b6\u6001\u7801\uff0c\u5982\u679c\u662f\u72b6\u6001\u7801\u90fd\u662f\u6570\u503c\uff0c\u5982\u679c\u662f\u63d0\u793a\u4fe1\u606f\uff0c\u90fd\u662f\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u5728\u4f7f\u7528\u679a\u4e3e\u7684\u65f6\u5019\uff0c\u5f80\u5f80\u662f\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u5f02\u6784\u679a\u4e3e\u7684\uff0c\u4e3b\u8981\u662f\u505a\u597d\u7c7b\u578b\u7684\u6574\u7406\u3002<\/p>\n

5. \u5e38\u91cf\u679a\u4e3e<\/strong><\/div>\n

\u5728TypeScript\u4e2d\uff0c\u5b9a\u4e49\u4e86\u679a\u4e3e\u503c\u4e4b\u540e\uff0c\u7f16\u8bd1\u6210 JavaScript \u7684\u4ee3\u7801\u4f1a\u521b\u5efa\u4e00\u4e2a\u5bf9\u5e94\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u4f7f\u7528\u3002\u4f46\u662f\u5982\u679c\u4f7f\u7528\u679a\u4e3e\u53ea\u662f\u4e3a\u4e86\u8ba9\u7a0b\u5e8f\u53ef\u8bfb\u6027\u597d\uff0c\u5e76\u4e0d\u9700\u8981\u7f16\u8bd1\u540e\u7684\u5bf9\u8c61\u5462\uff1f\u8fd9\u6837\u4f1a\u589e\u52a0\u4e00\u4e9b\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u91cf\u3002TypeScript \u4e2d\u6709\u4e00\u4e2aconst enum(\u5e38\u91cf\u679a\u4e3e)\uff0c\u5728\u5b9a\u4e49\u679a\u4e3e\u7684\u8bed\u53e5\u4e4b\u524d\u52a0\u4e0aconst\u5173\u952e\u5b57\uff0c\u8fd9\u6837\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u4e0d\u4f1a\u521b\u5efa\u8fd9\u4e2a\u5bf9\u8c61\uff0c\u53ea\u662f\u4f1a\u4ece\u679a\u4e3e\u91cc\u62ff\u5230\u76f8\u5e94\u7684\u503c\u8fdb\u884c\u66ff\u6362\uff1a<\/p>\n

\r\nenum Status {\r\n  Off,\r\n  On\r\n}\r\nconst enum Animal {\r\n  Dog,\r\n  Cat\r\n}\r\nconst status = Status.On;\r\nconst animal = Animal.Dog;<\/pre>\n

\u4e0a\u9762\u7684\u4ee3\u7801\u7f16\u8bd1\u6210 JavaScript \u4e4b\u540e\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n

\r\nvar Status;\r\n(function(Status) {\r\n  Status[(Status[\"Off\"] = 0)] = \"Off\";\r\n  Status[(Status[\"On\"] = 1)] = \"On\";\r\n})(Status || (Status = {}));\r\nvar status = Status.On;\r\nvar animal = 0; \/\/ Dog <\/pre>\n

\u5bf9\u4e8e Status \u7684\u5904\u7406\uff0c\u5148\u662f\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf Status\uff0c\u7136\u540e\u5b9a\u4e49\u4e00\u4e2a\u7acb\u5373\u6267\u884c\u51fd\u6570\uff0c\u5728\u51fd\u6570\u5185\u7ed9 Status \u6dfb\u52a0\u5bf9\u5e94\u5c5e\u6027\uff0c\u9996\u5148Status[\u201cOff\u201d] = 0\u662f\u7ed9Status\u5bf9\u8c61\u8bbe\u7f6eOff\u5c5e\u6027\uff0c\u5e76\u4e14\u503c\u8bbe\u4e3a 0\uff0c\u8fd9\u4e2a\u8d4b\u503c\u8868\u8fbe\u5f0f\u7684\u8fd4\u56de\u503c\u662f\u7b49\u53f7\u53f3\u8fb9\u7684\u503c\uff0c\u4e5f\u5c31\u662f 0\uff0c\u6240\u4ee5Status[Status[\u201cOff\u201d] = 0] = \"Off\"\u76f8\u5f53\u4e8eStatus[0] = \u201cOff\u201d \u3002\u521b\u5efa\u4e86\u8fd9\u4e2a\u5bf9\u8c61\u4e4b\u540e\uff0c\u5c06 Status \u7684 On \u5c5e\u6027\u503c\u8d4b\u503c\u7ed9 status\uff1b\u518d\u6765\u770b\u4e0b animal \u7684\u5904\u7406\uff0c\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u5e76\u6ca1\u6709\u50cfStatus\u521b\u5efa\u4e00\u4e2aAnimal\u5bf9\u8c61\uff0c\u800c\u662f\u76f4\u63a5\u628aAnimal.Dog\u7684\u503c0\u66ff\u6362\u5230\u4e86const animal = Animal.Dog\u8868\u8fbe\u5f0f\u7684Animal.Dog\u4f4d\u7f6e\u3002<\/p>\n

\u901a\u8fc7\u5b9a\u4e49\u5e38\u91cf\u679a\u4e3e\uff0c\u53ef\u4ee5\u4ee5\u6e05\u6670\u3001\u7ed3\u6784\u5316\u7684\u5f62\u5f0f\u7ef4\u62a4\u76f8\u5173\u8054\u7684\u5e38\u91cf\u96c6\u5408\u3002\u800c\u4e14\u56e0\u4e3a\u8f6c\u8bd1\u540e\u62b9\u9664\u4e86\u5b9a\u4e49\u3001\u5185\u8054\u6210\u5458\u503c\uff0c\u6240\u4ee5\u5728\u4ee3\u7801\u7684\u4f53\u79ef\u548c\u6027\u80fd\u65b9\u9762\u5e76\u4e0d\u4f1a\u6bd4\u76f4\u63a5\u5185\u8054\u5e38\u91cf\u503c\u5dee\u3002<\/p>\n

6. \u679a\u4e3e\u6210\u5458\u7c7b\u578b\u548c\u8054\u5408\u679a\u4e3e\u7c7b\u578b<\/strong><\/div>\n

\u5982\u679c\u679a\u4e3e\u503c\u91cc\u6240\u6709\u6210\u5458\u90fd\u662f\u5b57\u9762\u91cf\u7c7b\u578b\u7684\u503c\uff0c\u90a3\u4e48\u679a\u4e3e\u7684\u6bcf\u4e2a\u6210\u5458\u548c\u679a\u4e3e\u503c\u672c\u8eab\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u7c7b\u578b\u6765\u4f7f\u7528\uff0c\u6211\u4eec\u79f0\u8fd9\u6837\u7684\u679a\u4e3e\u6210\u5458\u4e3a\u5b57\u9762\u91cf\u679a\u4e3e\u6210\u5458\u3002<\/p>\n

\u6ee1\u8db3\u6761\u4ef6\u7684\u679a\u4e3e\u6210\u5458\u7684\u503c\u6709\u4ee5\u4e0b\u4e09\u79cd\uff1a<\/p>\n

\u6ca1\u6709\u521d\u59cb\u503c\u7684\u679a\u4e3e\u6210\u5458\uff0c\u4f8b\u5982\uff1aenum E { A }<\/p>\n

\u503c\u4e3a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u4f8b\u5982\uff1aenum E { A = 'a' }<\/p>\n

\u503c\u4e3a\u6570\u503c\u5b57\u9762\u91cf\uff0c\u6216\u8005\u5e26\u6709-\u7b26\u53f7\u7684\u6570\u503c\u5b57\u9762\u91cf\uff0c\u4f8b\u5982\uff1aenum E { A = 1 }\u3001enum E { A = -1 }<\/p>\n

\uff081\uff09\u679a\u4e3e\u6210\u5458\u7c7b\u578b<\/strong><\/span><\/div>\n

\u5f53\u6240\u6709\u679a\u4e3e\u6210\u5458\u90fd\u62e5\u6709\u5b57\u9762\u91cf\u679a\u4e3e\u503c\u65f6\uff0c\u5c31\u679a\u4e3e\u6210\u5458\u6210\u4e3a\u4e86\u7c7b\u578b\uff1a<\/p>\n

\r\nenum Animal {\r\n  Dog = 1,\r\n  Cat = 2\r\n}\r\n \r\ninterface Dog {\r\n  type: Animal.Dog; \r\n}\r\ninterface Cat {\r\n  type: Animal.Cat; \r\n}\r\n \r\nlet cat: Cat = {\r\n  type: Animal.Dog \/\/ error [ts] \u4e0d\u80fd\u5c06\u7c7b\u578b\u201cAnimal.Dog\u201d\u5206\u914d\u7ed9\u7c7b\u578b\u201cAnimal.Cat\u201d\r\n};\r\nlet dog: Dog = {\r\n  type: Animal.Dog\r\n};<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u4ee3\u7801\u7684\u7b2c\u4e03\u884c\u4f7f\u7528Animal.Dog\u4f5c\u4e3a\u7c7b\u578b\uff0c\u6307\u5b9a\u63a5\u53e3Dog\u7684\u5fc5\u987b\u6709\u4e00\u4e2atype\u5b57\u6bb5\uff0c\u4e14\u7c7b\u578b\u4e3aAnimal.Dog\u3002<\/p>\n

\uff082\uff09\u8054\u5408\u679a\u4e3e\u7c7b\u578b<\/strong><\/span><\/div>\n

\u5f53\u679a\u4e3e\u503c\u7b26\u5408\u6761\u4ef6\u65f6\uff0c\u8fd9\u4e2a\u679a\u4e3e\u503c\u5c31\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6210\u5458\u7684\u8054\u5408\u7c7b\u578b\uff1a<\/p>\n

\r\nenum Status {\r\n  Off,\r\n  On\r\n}\r\ninterface Light {\r\n  status: Status;\r\n}\r\nenum Animal {\r\n  Dog = 1,\r\n  Cat = 2\r\n}\r\nconst light1: Light = {\r\n  status: Animal.Dog \/\/ error \u4e0d\u80fd\u5c06\u7c7b\u578b\u201cAnimal.Dog\u201d\u5206\u914d\u7ed9\u7c7b\u578b\u201cStatus\u201d\r\n};\r\nconst light2: Light = {\r\n  status: Status.Off\r\n};\r\nconst light3: Light = {\r\n  status: Status.On\r\n};<\/pre>\n

\u4e0a\u9762\u4f8b\u5b50\u5b9a\u4e49\u63a5\u53e3 Light \u7684 status \u5b57\u6bb5\u7684\u7c7b\u578b\u4e3a\u679a\u4e3e\u503c Status\uff0c\u90a3\u4e48\u6b64\u65f6 status \u7684\u5c5e\u6027\u503c\u5fc5\u987b\u4e3a Status.Off \u548c Status.On \u4e2d\u7684\u4e00\u4e2a\uff0c\u4e5f\u5c31\u662f\u76f8\u5f53\u4e8estatus: Status.Off | Status.On\u3002<\/p>\n

7. \u679a\u4e3e\u5408\u5e76<\/strong><\/div>\n

\u8bf4\u5b8c\u5e38\u89c1\u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u6700\u540e\u6765\u770b\u770b\u679a\u4e3e\u5408\u5e76\u7684\u6982\u5ff5\u3002\u5bf9\u4e8e\u679a\u4e3e\u7c7b\u578b\u7684\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u5206\u5f00\u8fdb\u884c\u58f0\u660e\uff1a<\/p>\n

\r\nenum Day {\r\n  SUNDAY,\r\n  MONDAY,\r\n  TUESDAY\r\n }\r\n \r\nenum Day {\r\n  WEDNESDAY,\r\n  THURSDAY,\r\n  FRIDAY,\r\n  SATURDAY\r\n }<\/pre>\n

\u8fd9\u65f6 TypeScript \u5c31\u4f1a\u5bf9\u8fd9\u4e2a\u679a\u4e3e\u503c\u8fdb\u884c\u5408\u5e76\u64cd\u4f5c\uff0c\u5408\u5e76\u540e\u7f16\u8bd1\u4e3aJavaScript\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\nvar Day = void 0;\r\n(function (Day) {\r\n  Day[Day[\"SUNDAY\"] = 0] = \"SUNDAY\";\r\n  Day[Day[\"MONDAY\"] = 1] = \"MONDAY\";\r\n  Day[Day[\"TUESDAY\"] = 2] = \"TUESDAY\";\r\n  Day[Day[\"WEDNESDAY\"] = 3] = \"WEDNESDAY\";\r\n  Day[Day[\"THURSDAY\"] = 4] = \"THURSDAY\";\r\n  Day[Day[\"FRIDAY\"] = 5] = \"FRIDAY\";\r\n  Day[Day[\"SATURDAY\"] = 6] = \"SATURDAY\";\r\n})(Day || (Day = {}));<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8e\u8be6\u89e3 TypeScript \u679a\u4e3e\u7c7b\u578b\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

TypeScript \u5728 ES \u539f\u6709\u7c7b\u578b\u57fa\u7840\u4e0a\u52a0\u5165\u679a\u4e3e\u7c7b\u578b\uff0c\u4f7f\u5f97\u5728 TypeScript \u4e2d\u4e5f\u53ef\u4ee5\u7ed9\u4e00\u7ec4\u6570\u503c\u8d4b […]<\/p>\n","protected":false},"author":1479,"featured_media":35644,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-229493","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/229493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=229493"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/229493\/revisions"}],"predecessor-version":[{"id":229496,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/229493\/revisions\/229496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/35644"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=229493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=229493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=229493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}