打字稿中的枚举声明中的方括号是什么意思? [英] What is the meaning of square brackets in the enum declaration in typescript?

查看:129
本文介绍了打字稿中的枚举声明中的方括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览一个名为collection.ts的Angular ngrx项目中的一个打字稿文件,在那里,我看到声明了以下枚举常量.

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.

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;

为枚举常量提供一个值很好,但是我很困惑这个[Collection]在这里表示每个常量的一部分.这样写对枚举常量的值没有影响还是显示了其他内容?谁能解释一下?

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. Does writing like this have no effect on the value of the enum constant or does it show something else? Can anyone please explain?

推荐答案

您可能正在使用某些ngrx store或类似框架.这个枚举定义了动作,并确保您的动作键(字符串)在全局级别上是唯一的,因为最后,它们全部合并为减速器会做出反应的一大套动作. 为帮助满足该条件,通常将操作所要使用的类型放在方括号中键的开头.它只是一个命名约定,与打字稿完全无关.

You're probably using some flux/redux framework like ngrx store or similar. 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.

例如,

您可以具有实体BookCategory.对于它们两个,您都可以使用键Load Entity进行操作.为了区分这两个操作键,一种约定是放置诸如[Book] Load Entity[Category] Load Entity之类的名称.

You could have entities Book and Category. For both of them you could have action with key Load Entity. To distinguish those 2 action keys, one convention is to put names like [Book] Load Entity and [Category] Load Entity.

这篇关于打字稿中的枚举声明中的方括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆