当前位置:网站首页>Typescript enumeration

Typescript enumeration

2022-06-11 07:59:00 YY little monster

Details visible
1. Enumeration of numbers

 By default, it is numeric enumeration 
enum Gender{
    
    Male,
    Female
}
console.log(Gender.Male);
console.log(Gender.Female);

2. Numerical enumeration points of attention

//  The value of numeric enumeration is from... By default 0 Began to increase 
//  The value of numeric enumeration can be literal ,  It can also be a constant ,  It can also be the result of calculation 
const num = 666;
function getNum() {
    
    return 888;
}
enum Gender{
    
    // Male = 6,
    // Male = num, //  Be careful :  If a constant is used to assign a value to the previous enumeration ,  Then the following enumeration values also need to be assigned manually 
    // Female = 8
    Male = getNum(), //  Be careful :  If you assign a value to the previous enumeration using the calculation result ,  Then the following enumeration values also need to be assigned manually 
    Female = 123
}

3. Enumerating reverse mappings

//  You can get the original value according to the enumeration value 
//  You can also get the enumeration value based on the original value 
enum Gender{
    
    Male,
    Female
}
console.log(Gender.Male); // 0
console.log(Gender[0]); // Male

4. String Enum

enum Gender{
    
    Male = 'www.it666.com',
    Female = 'www.itzb.com' //  Be careful :  If a string is used to assign a value to the previous enumeration ,  Then the following enumeration values must also be assigned manually 
}
console.log(Gender.Male);
console.log(Gender.Female);

5. String enumerations note

 Be careful :  If a string is used to assign a value to the previous enumeration ,  Then the following enumeration values must also be assigned manually 
 Be careful :  Unlike numeric enumeration ,  String enumeration cannot assign an enumeration value with a constant or calculation result 
 Be careful :  Although string enumeration cannot assign values to enumeration values using constants or calculation results ,  However, it can use other internal enumeration values to assign 
const str = 'lnj';
function getStr() {
    
    return 'abc';
}
enum Gender{
    
    Male = 'www.it666.com',
    // Male = str,
    // Male = getStr(),
    Female = 'www.itzb.com',
    Yao = Female
}
console.log(Gender.Female);
console.log(Gender.Yao);

6. Heterogeneous enumeration

//  Enumeration contains both numbers and strings ,  We call it heterogeneous enumeration 
enum Gender{
    
    Male = 6,
    Female = 'nv'
}

console.log(Gender.Male);
console.log(Gender.Female);
console.log(Gender[6]);
//  Be careful :  If it is a string enumeration ,  Then the enumeration value cannot be obtained from the original value 
// console.log(Gender['nv']);
console.log(Gender);

7. Enumerate member types

//  We can use enumeration members as types 
enum Gender{
    
    Male = 'www.it666.com',
    Female = 'www.itzb.com'
}
interface TestInterface {
    
    age: Gender.Male
}
class Person implements TestInterface{
    
    age: Gender.Male
    // age: Gender.Female //  Due to type mismatch ,  So there's an error 
    // age: 0 //  Be careful :  Because the essence of numerical enumeration is numeric ,  So there is no error in writing a value here 

    // age: Gender.Male
    // age: Gender.Female
    // age: 'www.it666.com' //  Be careful :  If it is a string enumeration ,  Then it can only be the value of enumeration members ,  Cannot be any other value 
    // age: string
}

8. Union enumeration type

// 2.1 What is the joint type ?
//  Union type is to pass multiple data types through | Connect 
//  We can use an enumeration type as a union type 

let value:(number | string); // (number | string) Joint type 
value = 1;
value = 6;
value = "123";

enum Gender{
    
    Male ,
    Female
}
interface TestInterface {
    
    age: Gender // age: (Gender.Male | Gender.Female)
}
class Person implements TestInterface{
    
    // age: Gender.Male
    age: Gender.Female
}

9. Runtime enumeration

 Runtime enumeration 
 Enumeration is a real stored object after compilation ,  So you can use... At run time 
 And code like interfaces that are only used for constraints and static checks ,  It doesn't exist after compilation 
interface TestInterface {
    
    name:string;
    age:number;
}
enum Gender{
    
    Male,
    Female
}

10. Constant enumeration

 The difference between ordinary enumeration and constant enumeration 
 Ordinary enumeration will generate real objects 
 Constant enumeration does not generate real objects ,  Instead, use the value of the enumeration member to directly replace the place used 
enum Gender1{
    
    Male,
    Female
}
console.log(Gender1.Male === 0);

const enum Gender2{
    
    Male,
    Female
}
console.log(Gender2.Male === 0);
原网站

版权声明
本文为[YY little monster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110754014614.html