[英]What is the meaning of square brackets in the enum declaration in typescript?
I was going through a typescript file in an Angular ngrx project named as collection.ts
and there, I saw the following enum constant being declared. 我正在浏览一个名为
collection.ts
的Angular ngrx项目中的一个打字稿文件,在那儿,我看到声明了以下枚举常量。
import { Action } from '@ngrx/store';
import { Book } from '../models/book';
export enum CollectionActionTypes {
AddBook = '[Collection] Add Book',
AddBookSuccess = '[Collection] Add Book Success',
AddBookFail = '[Collection] Add Book Fail',
RemoveBook = '[Collection] Remove Book',
RemoveBookSuccess = '[Collection] Remove Book Success',
RemoveBookFail = '[Collection] Remove Book Fail',
Load = '[Collection] Load',
LoadSuccess = '[Collection] Load Success',
LoadFail = '[Collection] Load Fail',
}
/**
* Add Book to Collection Actions
*/
export class AddBook implements Action {
readonly type = CollectionActionTypes.AddBook;
constructor(public payload: Book) {}
}
export class AddBookSuccess implements Action {
readonly type = CollectionActionTypes.AddBookSuccess;
constructor(public payload: Book) {}
}
export class AddBookFail implements Action {
readonly type = CollectionActionTypes.AddBookFail;
constructor(public payload: Book) {}
}
/**
* Remove Book from Collection Actions
*/
export class RemoveBook implements Action {
readonly type = CollectionActionTypes.RemoveBook;
constructor(public payload: Book) {}
}
export class RemoveBookSuccess implements Action {
readonly type = CollectionActionTypes.RemoveBookSuccess;
constructor(public payload: Book) {}
}
export class RemoveBookFail implements Action {
readonly type = CollectionActionTypes.RemoveBookFail;
constructor(public payload: Book) {}
}
/**
* Load Collection Actions
*/
export class Load implements Action {
readonly type = CollectionActionTypes.Load;
}
export class LoadSuccess implements Action {
readonly type = CollectionActionTypes.LoadSuccess;
constructor(public payload: Book[]) {}
}
export class LoadFail implements Action {
readonly type = CollectionActionTypes.LoadFail;
constructor(public payload: any) {}
}
export type CollectionActions =
| AddBook
| AddBookSuccess
| AddBookFail
| RemoveBook
| RemoveBookSuccess
| RemoveBookFail
| Load
| LoadSuccess
| LoadFail;
Providing a value to the enum constants is fine but I'm confused what does this [Collection]
signify here as a part of each constant. 为枚举常量提供一个值很好,但是我很困惑这个
[Collection]
在这里表示每个常量的一部分。 Does writing like this have no effect on the value of the enum constant or does it show something else? 这样写对枚举常量的值没有影响还是显示了其他内容? Can anyone please explain?
谁能解释一下?
You're probably using some flux/redux framework like ngrx store
or similar. 您可能正在使用某些
ngrx store
类似的flux / redux框架。 This enum defines actions and ensures your actions keys (which are strings) are unique on a global level since at the end, they all get merged into one big set of actions that reducers react to. 这个枚举定义了动作,并确保您的动作键(字符串)在全局级别上是唯一的,因为最后,它们全部合并为减速器做出反应的一大套动作。 To help meet that condition, you would usually put the type that actions are for at the begining of key in square brackets.
为帮助满足该条件,通常将操作所要使用的类型放在方括号中键的开头。 Its just a naming convention not related to typescript in any way.
它只是一个命名约定,与打字稿完全无关。
For example, 例如,
You could have entities Book
and Category
. 您可以拥有实体
Book
和Category
。 For both of them you could have action with key Load Entity
. 对于它们两者,您都可以使用键
Load Entity
。 To distinguish those 2 action keys, one convention is to put names like [Book] Load Entity
and [Category] Load Entity
. 为了区分这两个操作键,一种约定是放置诸如
[Book] Load Entity
和[Category] Load Entity
。
As the brackets are within the value string, they have no intrinsic meaning as far as the enum
or TypeScript is concerned. 由于方括号位于值字符串中,因此就
enum
或TypeScript而言,它们没有任何内在含义。 The bracketed values are simply a convention for that specific project - they could be parentheses or braces, it doesn't matter - they may or may not be utilized in some other fashion. 括起来的值只是该特定项目的约定-可以是括号或大括号,没关系-可以或可以不以其他方式使用它们。
A similar syntax you may be thinking of or have seen would be a computed key, ex: 您可能正在考虑或已经看到的类似语法将是计算键,例如:
const propNam = 'test';
const obj = {[propName]: 2};
console.log(obj.test); // 2
However, these are not allowed in enum
declarations. 但是,这些在
enum
声明中是不允许的。 If you look at the compiled JavaScript from an enum
declaration, it does use computed keys to achieve reverse mappings (as explained down this page ): 如果您从
enum
声明中查看已编译的JavaScript,则它确实使用计算键来实现反向映射(如本页面所述 ):
TypeScript: 打字稿:
enum Enum {
A
}
let a = Enum.A;
let nameOfA = Enum[a]; // "A"
JavaScript: JavaScript的:
var Enum;
(function (Enum) {
Enum[Enum["A"] = 0] = "A";
})(Enum || (Enum = {}));
var a = Enum.A;
var nameOfA = Enum[a]; // "A"
But this is very different from the usage of brackets in your case. 但这与您所使用的方括号的用法非常不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.