简体   繁体   English

打字稿中的枚举声明中的方括号是什么意思?

[英]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 . 您可以拥有实体BookCategory 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.

相关问题 $在打字稿中是什么意思 - what is the meaning of $ in typescript 什么是枚举的 TypeScript 类型 - What is the TypeScript type of an enum 什么是@符号在typescript中的意思--Angular 2 - What is meaning by @ symbol in typescript --Angular 2 Angular 2 \\ TypeScript中export关键字的确切含义是什么? - What is the exact meaning of export keyword in Angular 2\TypeScript? 当我在ngFor中的方括号中写数组名称时有什么区别? - What is difference when I write array name in square brackets in ngFor? TypeScript:这个变量声明语法是什么意思? - TypeScript : what does this variable declaration syntax mean? 如果我尝试使用 Angular 中的方括号动态访问属性值,Typescript 将显示错误 - Typescript is showing error if I try to access the property value dynamically using square brackets in Angular 这个错误的含义是什么?声明实例方法后不允许声明实例字段。 - What is the meaning of this error “Declaration of instance field not allowed after declaration of instance method.” 打字稿中这段代码的含义是什么? public testOptions:“未定”| “是的”| “不”=“未定”; - What is meaning of this piece of code in typescript? public testOptions: “Undecided” | “Yes” | “No” = “Undecided”; /** @docs-private */ 在 angular 的打字稿代码库中是什么意思? - What is the meaning of /** @docs-private */ in angular's typescript codebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM