当前位置:网站首页>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);
边栏推荐
- 图数据库无缝集成Tushare接口
- Thoroughly remember the difference between ImageView background and SRC
- 2021-10-17
- Training yolov4 CSP model using coco dataset
- Several ways to avoid concurrent modification exceptions of lists
- TypeScript-头文件使用细节
- Summary of force deduction questions
- Three expressions of integers and their storage in memory
- Bladed入門教程(視頻)
- Closure and minimum dependency in database
猜你喜欢

C language - growth diary-04- preliminary exploration of local variables (local variables)

Method summary of creating deep learning model with keras/tensorflow 2.9
![[the most complete ENSP [installation diagram] in history!]](/img/1a/7d50d6e5c06a5e3ce6acab16ddb638.jpg)
[the most complete ENSP [installation diagram] in history!]

Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本

空间几何

mpi

How to output the percent sign "%" in printf function in C language

【 史上最全的ENSP【安装图解】!】

C wechat upload form data

Crawl Baidu Baipin dynamic page
随机推荐
TypeScript-键盘映射
TypeScript-接口和类型别名异同
Label the mask image not obtained through labelme
Use special characters to splice strings "+“
860. 柠檬水找零
C. Manipulating History(贪心/哈希/思维/好题)
如何开始参与开源社区
Tidb Cloud est en ligne sur le marché Google Cloud pour permettre aux développeurs du monde entier d'utiliser une nouvelle pile de bases de données htap en temps réel
[atcoder2307] tree game
134. 加油站
Note: JDBC
TiDB Cloud 上线 Google Cloud Marketplace,以全新一栈式实时 HTAP 数据库赋能全球开发者
Shell编程笔记
C language - Growth Diary -02- function
远程办公经验分享 | 社区征文
TypeScript-类型别名
如何做好空状态设计?来看这份全面总结
DAMENG 数据库登陆
Alchemy experience (model training of deep learning) the necessity of timely adjusting training parameters for some situations (the adjustment of learning rate LR is the primary) summarizes some metho
Introduction to operations research