TypeScript 编程语言的枚举,是 TypeScript 特有、JavaScript 没有的东西。枚举可以快速地将多个不同的值加上名称并群组化。

直接举个例子来说明 TypeScript 枚举的用法:

enum Level {
    One,
    Two,
    Three
}

console.log(Level.One);
console.log(Level.Two);
console.log(Level.Three);

以上程序的输出结果如下:

0
1
2

预设的枚举,可以直接定义出数个名称,称为变体(Variant),每个变体所代表的值即为它们的序数(ordinal),从 0 开始数。

如果我们要改变变体所代表的数值,可以在变体后面使用 = 来指派数值。一旦有变体的值是明确指派某个数值的话,这个变体后面的其它变体的值就会预设是这个变体的值加 N,N 为这个变体与其它变体的序数差。

枚举的变体可以透过在枚举名称后加上句点 . 的方式来读取,但它的值无法被改变。

例如:

enum Level {
    One,
    Two = 10,
    Three ,
    Four,
    Five = 20.5,
    Six
}

console.log(Level.One);
console.log(Level.Two);
console.log(Level.Three);
console.log(Level.Four);
console.log(Level.Five);
console.log(Level.Six);

以上程序的输出结果如下:

0
10
11
12
20.5
21.5

如果指派给变体的值不是数值的话,则这个变体之后的值必须要手动指派,不然会编译错误。

例如:

enum Level {
    One,
    Two = 10,
    Three,
    Four,
    Five = 20.5,
    Six,
    Seven = "25",
    Eight = 30,
    Nine
}

console.log(Level.One);
console.log(Level.Two);
console.log(Level.Three);
console.log(Level.Four);
console.log(Level.Five);
console.log(Level.Six);
console.log(Level.Seven);
console.log(Level.Eight);
console.log(Level.Nine);

以上程序中的 Eight 变体必须要手动指派值,因为 Seven 变体的值不是数值。

枚举也是一个类型,例如:

enum Weekday {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

function isWeekend(day: Weekday) {
    switch (day) {
        case Weekday.Saturday:
        case Weekday.Sunday:
            return true;
        default:
            return false;
    }
}

console.log(isWeekend(Weekday.Monday));
console.log(isWeekend(Weekday.Saturday));

isWeekend 函数有一个类型为 Weekday 的 day 参数,如果 day 参数的值是 Weekday.Saturday 或是 Weekday.Sunday 的话,就回传 true,否则回传 false。

以上程序会输出:

false
true

如果枚举的变体的值是数值的话,我们可以直接利用数值来表示该变体。例如以上程序可以修改如下:

enum Weekday {
    Monday = 1,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}

function isWeekend(day: Weekday) {
    return day === 6 || day === 7;
}

console.log(isWeekend(1));
console.log(isWeekend(6));

定数类型

定数也可以直接当作类型来用,常拿来代替枚举。

例如:

function isWeekend(day: 1|2|3|4|5|6|7) {
    return day === 6 || day === 7;
}

console.log(isWeekend(1));
console.log(isWeekend(6));

以上程序,isWeekend 函数的 day,只能传入数值 1~7。

再举一个例子:

let o: { a: 1, b: 2 } = {a: 1, b: 2};
let a: [1, 2] = [1, 2];
let f: (n: 1) => 2 = (n) => 2;

以上程序,限定了 o 对象只能是 { a: 1, b: 2 },a 数组只能是 [1, 2],f 函数只能是 (n: 1) => 2。

再来一个例子:

let o: { a: 1 | 2, b: number | string } = {a: 1, b: 2};

以上程序,限定了 o 对象的 a 属性的值只能是 1 或是 2,b 属性的值只可以是数值或是字符串类型。

TypeScript 在推论 const 声明出来的常数类型时,如果常数值是基本数据类型,则其类型会被推论为定数类型(即直接将值当作类型),表示其无法被更改!

举例来说:

const a = 1;
const b = "Hello!";

以上 a 常数的类型为 1,b 常数的类型为 "Hello!"。

常数枚举

我们再来看这个例子:

enum Level {
    One,
    Two,
    Three
}

console.log(Level.One);
console.log(Level.Two);
console.log(Level.Three);

以上程序在经过 TypeScript 编译之后,得到的 JavaScript 代码如下:

var Level;
(function (Level) {
    Level[Level["One"] = 0] = "One";
    Level[Level["Two"] = 1] = "Two";
    Level[Level["Three"] = 2] = "Three";
})(Level || (Level = {}));
console.log(Level.One);
console.log(Level.Two);
console.log(Level.Three);

如果我们确定这个枚举只会在这个程序项目下用到的话,可以在 enum 关键字前面加上 const 关键字,使其成为常数枚举。

例如:

const enum Level {
    One,
    Two,
    Three
}

console.log(Level.One);
console.log(Level.Two);
console.log(Level.Three);

以上程序会被编译为:

console.log(0);
console.log(1);
console.log(2);

看到以上的 JavaScript 程序就可以知道,我们应该要尽量使用常数枚举,让 JavaScript 程序在运行阶段不会有额外的开支(overhead)。

总结

在这个章节中我们学会了 TypeScript 提供的枚举用法,以及定数类型的用法。在下一个章节要来学习 TypeScript 提供的泛型(generic)用法。

下一章:TypeScript 编程语言的泛型。