The enumeration signature action uses the keyword enum to define the type. Such as

// enum.ts
enumFriends {Zhang SAN, Li Si, Wang Wu};Copy the code

// enum2.ts
enumFriends2 {Zhang SAN, Li Si, Wang Wu};console.log(Friends2[0= = ='Joe')
console.log(Friends2['Joe'= = =0)
Copy the code

The compiled

// enum2.js
var Friends2;
(function (Friends2) {
    Friends2[Friends2["\u5F20\u4E09"] = 0] = "\u5F20\u4E09";
    Friends2[Friends2["\u674E\u56DB"] = 1] = "\u674E\u56DB";
    Friends2[Friends2["\u738B\u4E94"] = 2] = "\u738B\u4E94";
})(Friends2 || (Friends2 = {}));
;
console.log(Friends2[0= = ='Joe'); // true
console.log(Friends2['Joe'= = =0); // true
Copy the code

As you can see, enumerations can get values with subscript indexes, and the key and value of an enumeration can be mapped backwards.

Manual assignment

// enum3.ts
enumFriends3 {Friends3 =30, li si =18And fifty};console.log(Friends3['Joe'= = =30);
console.log(Friends3['bill'= = =18);
console.log(Friends3['Cathy'= = =19);

enumFriends3_1 {zhang SAN ='30', li si ='18'And fifty ='9' };
enumFriends3_2 {zhang SAN ='30', li si ='18'And fifty =9 };
enumFriends3_3 {zhang SAN ='30', Li Si, Wang Wu};// 0.0.8/enum3.ts:8:30 - error TS1061: Enum member must have initializer.
    // 8 enum Friends3_3 {friends3 = '30', friends3, friends3};~ ~// 0.0.8/enum3.ts:8:34 - error TS1061: Enum member must have Initializer
    // 8 enum Friends3_3 {friends3 = '30', friends3, friends3};
Copy the code

The compiled

// enum3.js
var Friends3;
(function (Friends3) {
    Friends3[Friends3["\u5F20\u4E09"] = 30] = "\u5F20\u4E09";
    Friends3[Friends3["\u674E\u56DB"] = 18] = "\u674E\u56DB";
    Friends3[Friends3["\u738B\u4E94"] = 19] = "\u738B\u4E94";
})(Friends3 || (Friends3 = {}));
;
console.log(Friends3['Joe'= = =30);
console.log(Friends3['bill'= = =18);
console.log(Friends3['Cathy'= = =19);
var Friends3_1;
(function (Friends3_1) {
    Friends3_1["\u5F20\u4E09"] = "30";
    Friends3_1["\u674E\u56DB"] = "18";
    Friends3_1["\u738B\u4E94"] = "9";
})(Friends3_1 || (Friends3_1 = {}));
;
var Friends3_2;
(function (Friends3_2) {
    Friends3_2["\u5F20\u4E09"] = "30";
    Friends3_2["\u674E\u56DB"] = "18";
    Friends3_2[Friends3_2["\u738B\u4E94"] = 9] = "\u738B\u4E94";
})(Friends3_2 || (Friends3_2 = {}));
;
var Friends3_3;
(function (Friends3_3) {
    Friends3_3["\u5F20\u4E09"] = "30";
    Friends3_3[Friends3_3["\u674E\u56DB"] = void 0] = "\u674E\u56DB";
    Friends3_3[Friends3_3["\u738B\u4E94"] = void 0] = "\u738B\u4E94";
})(Friends3_3 || (Friends3_3 = {}));
;
Copy the code

The way

  • Assignments are numbers that can be partially assigned;
  • One of the assignments is a string, so all members are assigned;

Q: What if the value is manually assigned to a string?

// enum4.ts
enumFriends4 {zhang SAN ='30', Li Si, Wang Wu};enumFriends4_1 {zhang SAN = <any>'30', Li Si, Wang Wu};enumFriends4_2 {Friends4_2, Friends4_2 = <any>'30'And fifty};enumFriends4_3 {zhang SAN, LI Si = <any>'30'And fifty ='30' };
enumFriends4_4 {Zhang SAN, Li Si, Wang Wu = <any>'30' };
enumFriends4_5 {Friends4_5, Friends4_5 = <any>'30', Wang Wu = <any>'30' };
enumFriends4_6 {Friends4_6, Friends4_6 = <any>'30'And fifty =30 };


// 0.0.8/enum4.ts:1:28 - error TS1061: Enum member must have initializer.
    // 1 enum Friends4 {friends3 = '30', Friends4, friends5};
// 0.0.8/enum4.ts: 1:32-error TS1061: Enum Member must have Initializer.
    // 1 enum Friends4 {friends3 = '30', Friends4, friends5};~ ~// 0.0.8/enum4.ts:2:35 - error TS1061: Enum member must have initializer.
    // 2 enum Friends4_1 {Friends4_1 = 
      
       '30', Friends4_1, Friends4_1};
      
// 0.0.8/enum4.ts:2:39 - error TS1061: Enum member must have initializer.
    // 2 enum Friends4_1 {Friends4_1 = 
      
       '30', Friends4_1, Friends4_1};
      ~ ~// 0.0.8/enum4.ts:3:39 - error TS1061: Enum Member must have Initializer
    // 3 enum Friends4_2 {Friends4_2, Friends4_2, Friends4_2};~ ~// 0.0.8/enum4. Ts :4:28 - Error TS2553: Computed values are not permitted in an enum with string Valued members
    // 4 enum Friends4_3 {Friends4_3, Friends4_3 = 
      
       '30', Friends4_3 = '30'};
      
Copy the code

Above, let me give you an example

  • Line 1 is an error because only the first member of the string is assigned and no other members are assigned;
  • Lines 2-3 report an error because of a type assertion<any>Member assignments follow;
  • The error in line 4 is due to type assertion<any>The following assignment is not a number;
  • The remaining lines are not reported because of type assertions<any>The following members are assigned (and);

conclusion

  • Assignments are strings, all of them;
  • If the assignment part is a string, use a type assertion, which cannot be followed by a member assigned to a string.

I am aware that there are many complex scenarios that are not listed, and I hope this article will give you a brief introduction to enumerations. I also hope that this article will make you aware of the importance of drawing inferences and thinking for yourself. Of course, for other scenarios, in the future, once encountered or updated to this article, continuous improvement and self-improvement (a qualified programmer’s self-rehabilitation).

This code Github

You can…

Previous: Typescript primitives

Next: Typescript classes

Contents: A primer to Typescript’s short book