枚举
枚举是 TypeScript 的少数功能之一,它不是 JavaScript 的类型级扩展。
枚举允许开发人员定义一组命名常量。使用枚举可以更轻松地记录意图,或创建一组不同的案例。TypeScript 提供基于数字和基于字符串的枚举。
数字枚举
我们将首先从数字枚举开始,如果您来自其他语言,可能会更熟悉。可以使用 enum
关键字定义枚举。
enum Direction {
Up = 1,
Down,
Left,
Right,
}
上面,我们有一个数字枚举,其中 Up
用 1
初始化。从那时起,以下所有成员都会自动递增。换句话说,Direction.Up
的值为 1
,Down
的值为 2
,Left
的值为 3
,Right
的值为 4
。
如果我们愿意,我们可以完全不使用初始化器:
enum Direction {
Up,
Down,
Left,
Right,
}
9UxhulP3F8WqFqJ2xhY1ff6iQ6XXnlNWxUZFJR/lPy5idy6Z4wFL9VWEH+8P2Ce8LOR8Q7IDsBqfuOaHaBllqHU+XgKrnBNN6029GyaCbm7sXSy+7wdebbUSpL5kSe2oSvwOlqpZWdyUpziCiNlMJrM5rc1UuKeVbSRYxvBaIisPmh2/c8TT9YAO287EyVfCbiUcn/dz6lAzWJB2fPvgAWT08mpu1mWdAw1Nu/IamnNpcgp0sr8nm3z/iCMJR0S4dThqpjwKyvgcTjeXeGpOkzGRkitUEI1Lbl6wA9B7xd3a/Yu3PQWKNo9iIjLtCx1e9NxC7cESKNl4Hi0b9CmetfBXzJEZpfhhdh4mqUs57NI=
EhQA7FeweC4DF2bAHIYIeeuGRYZULN7WnNW0G6wLy7WkLvXsFbmpPwFWCsFLUxiEohjWq0of/vWnIlYUxrrehq/TV4VDJ4mBs7S/noF3TLLs3N/eVnOBfGBJITtlqX9bq2OhIw2YBmhfvsSkqsUKwsvnVh4j62LweJ3WpnTptO0=
enum UserResponse {
No = 0,
Yes = 1,
}
function respond(recipient: string, message: UserResponse): void {
// ...
}
respond("Princess Caroline", UserResponse.Yes);
SaOQp66UB9/ZLeWqylu9MDNv6vFlHkGQ3vLZB2YckWwSBh2CdAd5pRWSzKPYaj/Z2xtIAQruY/VhU5Wjj0kPfmuOppor2zKWZBz+snHVw3l18UTVLMN5Po0xIjxUXiq8VdJZWLm5MXFpvSDjhPi7gueiI6eLmvFSlfHPSZtVC4X5K6hhxSsn7/HunZeUNwq5KsRQbzLzzsWFhoYpZkwIIlkcfYjq0uFINnkF5ZNiKrRJtbeNcXZx6+W2A86sj/PayonXrobKwMuilSaogXKloRAgFx4tlwpqGvkTtq6tPz5BITBwA8zdOd9PuLipJc1bfH323hhxE59dPG5IpjGmnOCy+qoiFLZK+R3PuKhOaoVq0R7RotFJfZoimAZi2yVPReOlIlSIfd5Cj3uEsU/hy5/D1gAzZDwsNVPzPJhJz8j74KyuOkAbkDb2ztWG7E/MzAYGTwWVGRpybiWauHBPmg==
const getSomeValue = () => 23;
enum E {
A = getSomeValue(),
B,
}
字符串枚举
6wataNbs6Bplti40YBKq6pmgMl3pTMC8fazHYu3xuPQTU3J9571hlF9ifXKFdpvFRodAQZe3AFDaL3TPX1DeIYEfsbTYEMXfGanNBg34u9s19sGDKPAHBr92/LypypswjMSEj52ZIcBNBqMmCq1wkJcV/O40ZSeIHwaUdVAlgcK3uRKtDdPJa7jAa+NTarJDXllwBSFf6yGKhcGcrUlHA932Y0kB37Ph/F/ufvpAqTsV1IGwU1kDD1MbaHCHmPMG2yIWUcPre7idoLkxC9oL+L8RXJK9IeCxOdrQwld41OXqOuZM8H2g7sk+4BW7fsoCtD/pfLtfjnvbiKH67wO6Wv2x1uXh4TeLPUUgDN9PY/o=
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
uNR/JGp0PGor7yx6D39ngddXTzsKVnakvLc26iI5QpZGq0wLxjpfGDHlHjt7XaxFS31DInudkgIpBPkC8PENu7XlsTkVrtpa1+UzdvaKQ6DDkdvPVsXtw6oTuiieIM81YodMIzJdbrxakxyCQCPJgXWUmb8/IaPohuecPIG5hF6bCPL0ohIHZi7qZUgVvR+M/s5Zl73XPURR9Thd/vvGqpnNmWmJNAmrS4QpYiQGXesQLDPVlz/qQ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WpIHqojWPnv6qBPU56IYwaqZmBypOBt+wTEg76ZSxilyVzPbzFcf975S0/whcVZDpOLEIUNAC+9XIRDnpZL9Das4Z0pnmCTpf/iPNqUrei0smlyfrEIoVQlXp/K5pH4Z+tn4fNwNFzpdocVTWt4ZQ5Ef/XlSP3u+TVRaXQXdbClrWROwXf/oDeYL+jrW1ZlY1Pm0iGn1dVvhCZoCtYsep6imgCdZTK8qvFbfX4t9bG0MmlgOCNdTy/SNVqyjLxXTzTg3QHMY6sOBBXUBfge07bFHGjDUHt4WCDGGbarz9lUtrNfprJkLofBhQawRhsUY8003RFGelaC3a0LtVfSmgObnXHzHaFsbvmvcOdR3EdNbFC9ALAFISFATbCqftl/Aq7GyF5z19E2Fg17KQ/AcA1yEK4/6k6cSsFSopWwHRH4xgthKAr8uLIa63BMZZsH7ohm50qCp+G8bUL7OV8udlrfKC9T5lKd6YQX8k1p0Cnz1aHqRqKYp6AeXXwqWPOzOXANkODcRdaRYoGC/uReHhwyDsdnxW7OCcQAp2aY304yiKq5YkA2GKqpzqX35No5u+ylw3yJpuI4ZzRnEG1t91RXSJJAdbPLb2cK25zp0mpD0ovxuLsA4zg4UmpjtZzs9Nq+2JBonpHmAVBYW+HHaKpGsxMpCU+MOV8hlvR5jJumSmX5L7ojxBM1tK1d0akZwd+K//chy2ZUb3jD4Ne2hqOnwu1vbQ1tXFVuJXZSMG1Nc7Bs3+c3rAwj9H69pqxah9juMWxFS030qQJEFtlGwdikYxncj9QhMua2zXEU741mCTkllohUr2P0WGUJf/HQTZndzYY0tQhye1fYSPPzHtwE/3PimEnx/3oDUMo=
ZuQv5Ks5QY4BX6bJfiqQD9YgtSzkQDCki5NBcfIWuFeQYp1JsG6tZzAzkRrnsuLzQoWb2qGdLI9fEhac+8ly8A==
enum FileAccess {
// constant members
None,
Read = 1 << 1,
Write = 1 << 2,
ReadWrite = Read | Write,
// computed member
G = "123".length,
}
联合枚举和枚举成员类型
iHvIq3opwngnsAfc4Mp4HDSt73JGKmUvRcAwTilhHY3tk1y6Zm2t6iW/TLkZsnrVgeBS/c8P+5tdF2aLMNl5zMEzP9cfBhtWA32cpStwk/g8KqPDXEpUrIQ1r7z6bciHyw2JayyI8I+6FQGfsbZQMERwn6eyA8mF1wu/2Gf/oQTNOVcV5XvwD2/QKkQVVbiBEh5SHA6wvyF5ONyhV6q6AySeTihBsf+owyyr0fiYJyY=
- pnQ/JUlFcuNzIR2BVz7mA+8Ii+eaBunEH7bJW/qJGi+fjmNvzJkuOKabhi51NDPwLOE9qLQIaAm1kaE2Bozy5aGcOJaVz+byZIPG/kprTL1LnOij6X+r9hX+5FKqjAchs2seJeLZFDt3iTWhsydCP1wIIUSARPNizYrx7GgpO+tcph/OFOfBqzrEqVtqzuXXL2Zkw9nGOCb0IyferZrwbv8WppvkxqvYFmKXX2f+B+6p5+/xxjW/PozITqFJh0H6e4AbC2JzSM+RpCOuIUpqOtDEOBiks9ozCneaadPAL9KPqde02fvCQcQIXdLqXFsPCfLCWy3Ztg6x4OIYrlClHzb14sU3QgdsxfTS94Drbir4kaOyMtxVefbig49nnejx
n6UiZ7XNlmglUmlidHJ0i+3rPw0ohNkMuATMWeHG7CirDkHTlx1mHWSiIREmU+jHutQeDcPxFhh9tanXP9AWhTBo7oDVgndMZ1Ey6lcNjUHLlSyZo2Xz9X5/9LW7LPf5tx1S2BhxyedTWtGYJCD9cA==
NWtiN2FVkrbJJ6REyJxT+JMt6gtDMR3sKjAO3cEQkWMJSCuWN5SVeGCN16mqdpzSRs+my/aypPyEvoVmtY/slS0VpRBwQKIo+lPalO2yXts5/bd2Uo2jTh81eT2anIp6mTuksdGDr107Y1ygk24kWw==
enum ShapeKind {
Circle,
Square,
}
interface Circle {
kind: ShapeKind.Circle;
radius: number;
}
interface Square {
kind: ShapeKind.Square;
sideLength: number;
}
let c: Circle = {
kind: ShapeKind.Square,
radius: 100,
};
uG2V00yJDRHMTUV23j2wmLm8oIpJtuSHXLI3f8E2eosy7aOMztgjDhYmHWaJb/7+IkzZQkFLIuKESEySIGyWq95CtrldBnVaeStxp8wfckk6jN2fJuRn+fw8TCOmM8rPk1eZ0HV1uwJE+T0J+31YT/iQlsCjwDZ/bBsbzqpAo+DmYBUI5dQeo/jY77IqWQy5QxkDn45S4LdLX5eVrEdPMyr5nXoM4FA6B6jpko6oOP4m0L6wimllnTgcLThZNS+H3uewYkUICHLbkG4pNLcsRXUITqyJHiqKooVJSPI9OYRHSOfoACqnzGzljkL4FzpHLPkBwX/a3RoX4m2GQF4yzIs6m/BHXB2rCGapMTNMoQ9aLsXszuqXyCrcbLwZjqn1
enum E {
Foo,
Bar,
}
function f(x: E) {
if (x !== E.Foo || x !== E.Bar) {
//
}
}
vrYLwZ0hleOSa64d6678KHqL8oyHPeyKgTCs3okVPr2avn9zsJFOVcl9SvDMkB9N5cxhpVLW/JdhL0JF5H1zQ3/M5rYD6cX/c/Nf8IAlp3r96PaNW/CRVNVQGVMnuynSoWknCRByAWGlqb9/4Rojt6uYWeoP2gIp3cNqKH0hMILz0w3LWTw2CIt0zQnuxCTe34fUYmrFC8n2udLYoQr/epDNWJNVwAPOdEX5cUJJ0tcn6TfyGXnfmie8OudsPD/dj6sHcG/JyXUvYk42kNnsmgMUPyRS3V1ae7/2i2xqUu6zJpnfkLg3YVXa0S5Qc8xVqPGcFOeimCp6sVzQaNh7An9oUe4pwmfQxY7gjPej9K8xqinMOAejud0+AzRIjYjwJ94NB8aQQIzmNTZBnzqPBPRxokseeo13FzZBXMiR+O7L18ut1nLsStFsKlFNdEE+
运行时的枚举
QxbMT72gzJmgXTR3udn3dl7bZlm0zvTr8MS3PSZgKOZnO8QQTDFHkW8I1JUXCE+cLe6VeKDdFmBGmbOsy8zQqg==
enum E {
X,
Y,
Z,
}
Ro8TXDlQdLwDqSFCrDMkoor8upbbX+eCRC/7Z68UJQ8=
enum E {
X,
Y,
Z,
}
function f(obj: { X: number }) {
return obj.X;
}
// Works, since 'E' has a property named 'X' which is a number.
f(E);
编译时的枚举
ye4+vYmfdufV0FpqL1pVZ9KoKaMSFkCGXbBy7Aa8NQgxAUP7k6i//qSIZ9cexcCkQGiapKhcxG13NhI5ZzXUeAtSbjowIDin0y9HHviAWDEREZrB8fupzn3U99umtnkyRzDJzNSGOKW50Q5eC46RSL0r9ksfJjDmThHJPB/JUH4APtDsheuHYXJuX/piBhwpRQPybd7cFGQtc6kH0bjm1DN1HfCF5iznVR3+dKo4SvgQilMCXD5o/Qwp3TiNXQG4wI1UUFP0y31SBy09O5RTfN0otcpCWVSjnPnbAz0VpZXum8V/8m7uPbKh1rgI3PXX
enum LogLevel {
ERROR,
WARN,
INFO,
DEBUG,
}
/**
* This is equivalent to:
* type LogLevelStrings = 'ERROR' | 'WARN' | 'INFO' | 'DEBUG';
*/
type LogLevelStrings = keyof typeof LogLevel;
function printImportant(key: LogLevelStrings, message: string) {
const num = LogLevel[key];
if (num <= LogLevel.WARN) {
console.log("Log level key is:", key);
console.log("Log level value is:", num);
console.log("Log level message is:", message);
}
}
printImportant("ERROR", "This is a message");
反向映射
wVib5IriX0xyht0lyT1FF2uhI/Ix460/QeY9Xp1j1by2ANKam/4+jgYlRx4jXxYoUEmksnWG/B24FthwBm2bfQc5HPy4nWV9E4AL/yW3ryoKxvdLDPrlf3lFp2eyyMmXkbMrG3WyXzxGCydZaQPoaU7954zxSfPUo3+jhBBFt3QMfz3N+eK/aGe/KgJyi1TwKkd//2dZVGpzH7+CfLSXKw==
enum Enum {
A,
}
let a = Enum.A;
let nameOfA = Enum[a]; // "A"
rV8t7gtFiYWbKhz7fAq5Uhp4UhCBtRH95q7luLBRqX7QS3JYhFP+8RgHFSIletXd
enum Enum {
A,
}
let a = Enum.A;
let nameOfA = Enum[a]; // "A"
qakDH7bltBb0bpq4ubn6yW5cuCQiB4mK8FANAj7G2wMNWxZIeriwaEXvnTRCk2xe6EWGTmIPaWpK5EFi9HeoeU5W9RWuErw23kJI5PfFzTlNrJeTNpIoiJgYmNByyCOmYT7SE7moO+grtFlW4C7dIhtmcdi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O+gifPz463I3ce2YtmJntR7A=
const enum Direction {
Up,
Down,
Left,
Right,
}
let directions = [
Direction.Up,
Direction.Down,
Direction.Left,
Direction.Right,
];
kS/qYOD7dOu+l5m7ki8yEJ3FPDSkYJRbhv+aU/KCxDI=
const enum Direction {
Up,
Down,
Left,
Right,
}
let directions = [
Direction.Up,
Direction.Down,
Direction.Left,
Direction.Right,
];
常量枚举陷阱
FL4ByHdOIGyV4ifaLV+iklfm0Vno0Uexc2veANWuvNsw857vOW1RwMgqs3RR5Td/yXdgaeIzsxBMb0FUp/nZU1bv52BgHgIVQpc/JT0xzpjGrs3IopHVgCWysF8Q3yxLcxN8oJEkiuLTpu/qrAa4UZ7ey+6kZ9q+omXngGRncSwvYvcGrp72uxMbOhlj07HhXWVSK4cLX8NIgeKlfedPZ8PJoWny0p9cdJVsa0R7NiCol/nJL9n3MLIycE/VX3TMUmv9CWaL2tVdbD0WQY6cigFWe0K4hWIqR+jyl1EWQxGfn60H1asP+a/pMbDdBD4n/0XiRvB935HOLrrG30xqjPaqXKX4jc4fMVVyr1u8xJ2ncbRbnI2NAnRp8yfpJziumALSytF5o7PkVlh7g4N9GEd0OF4gSWib/5zELml3UA2oLbev49ReGKN1aUTbU/oLGGMi2fMPTL41G9nxnwgSaXAv1MW660dWfKd0Lu6at/pJgTriTncM0qIXf/SNAT1sRsSkiL+r4Rh2TR2j+XlcHm8t6maWAfs3Jv589QknNjc=
- 由于
isolatedModules
文件 中列出的原因,该模式从根本上与环境常量枚举不兼容。这意味着如果您发布环境常量枚举,下引用者将无法同时使用isolatedModules
和那些枚举值。 - 您可以在编译时轻松地从依赖项的版本 A 中内联值,并在运行时导入版本 B。版本 A 和 B 的枚举可以有不同的值,如果你不是很小心,导致 意外的缺陷,就像走错了
if
语句的分支。这些错误特别有害,因为通常在构建项目的同时运行自动化测试,具有相同的依赖版本,完全忽略了这些错误。 importsNotUsedAsValues: "preserve"
不会忽略用作值的 const 枚举的导入,但环境 const 枚举不保证运行时.js
文件存在。无法解析的导入会在运行时导致错误。目前明确省略导入的常用方法,仅类型导入,不允许 const 枚举值。
zfsCVK/7jtoySDosp+llRctnR+RqBm6CVCn9JC1oSL5w3Wp0OARkpD6ykBMiFA3I
+XIgKGF/oWPvahVhLX5IGioNyGYwRXiZ6q1sesa7t2m5CvW9bzn/zS37Hg6inn/t+RFrfBrFNQHrNCahjnUHVEiH+hy7lrQHDaIJvg7LUVv5IquUtmO+/Zv5A/g45yfA1ib7Xk6u0vXp5bveBsdC9Tq4Kf9xxZKeqt5M3GBpBYp9K4IRHX8HkIA24o2wy5DFZH0ztXKWif2IkL+c6XYWvsD2dom/npuWa+AN8VbQGz9qMB1OQPMeiaxDsaaQijp1sV9UAA4yeYTKUlbX/SflCA1cJnsoIAgmu8qO8d/URsqvhKemu6bb+W3I/fy2SWkZtuK/KsdWLPKNdRfGO2dS9nUUtb+pYyk3r/MKY82iN0/k26oiJIufIn/I8w3TGrSrh/Abhcuks85X/bJixtiTkawTNN8ckSYS/aLlLS+nbSFIxMo5nK9U2aY7RUty2yI+k7vcLOgChx4mdr0slOwtqoFSSkw779WeX3SMHKIOAqTLki9QcsedwuKbuIMtJkcm2cOCVKJf7CPQYbTj/Y2j3mLmWX999wEHvbRK8tCHSBF0jRI4LLO3ICnBD2LGSsOBf8AeL5oeaLiSLfwORrH1saQkVCVrLDyDzzLrPsbRhRHfIHNfklbWgS89PPYGiP+prKucnCZNqJ6zUIpVZjUxJq+cQ6XIaNubn7FP+jUCRVXYAzEVLuuscLfzUZx95tboAuPvDnKBWTz36Ulf81r4BYjNR6SrVJPO/QZsOQNIN8T08TiZbope2KMdELsPy8SfyRD4kCU5g5otN05fyGAyuH8lq+3EyAEFDX2kJ0zZt6WAHIK16bLrfzxDdV1QR5l4qTxfh/TGKwEjN2Geum2yqVwFvuqSEbw8Ru90EqVF88iszXQjn97UMk0N6q8/ZJBOwf2QilFKhIbg296HkACpQ4sBaHJl/cAsJjV+LDr35ppvFMHECUYKYV8DkrG9wmh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pnQWJ8WfwBBcBlvnsk/nzvBgVUco7PQKX7QcPILlC9MbN1aNwGmd8hr/x0gypdBa0XEaFMVgK0431qDYYflvH87mp86dzxddvyNfaeJguhTZbTiqpact7cbLAL/poNk3YHpReQ==
const enum EDirection {
Up,
Down,
Left,
Right,
}
const ODirection = {
Up: 0,
Down: 1,
Left: 2,
Right: 3,
} as const;
EDirection.Up;
ODirection.Up;
// Using the enum as a parameter
function walk(dir: EDirection) {}
// It requires an extra line to pull out the values
type Direction = typeof ODirection[keyof typeof ODirection];
function run(dir: Direction) {}
walk(EDirection.Left);
run(ODirection.Right);
H/M2cH0LyAiVxfA0WhGycehb/DXZr3dX851hHKAW5XHxzGqLZuL48XKi/7mPkzNlrDncR8X9DJY8nrCJvm75P/D+HyJLSLy9mh6E7BdUa9NeCOV96QBM9EXxYLsm8YIoTh4u2vzXUxqdRSp+9bCiVY5HyC2TQ/woLoV/HJCptrqNlIsY4pjFsyWZ55i3JZMLGh//ZRF3V61QrHCfVME1RTVOvaIPxWsk9CdobhUcLSknpZTaOZwtFn1t2AbdxUYFFk96xK51+srwehl9qffABycdMA5RBw1Md6Razh5iZCd1uishqtdRQWP9BHyGiTUhe4vdMjn2EIe2Et3jKJ/LGvHL8GcGSqve9WWBWPY+ckJWYgn1issTEc1wuSfKGWuQ