https://github.com/reforms/ts-jenum
TypeScript Enum like java.lang.Enum
https://github.com/reforms/ts-jenum
enum enum-like-java java jenum tools ts typescript typescript-enum
Last synced: 14 days ago
JSON representation
TypeScript Enum like java.lang.Enum
- Host: GitHub
- URL: https://github.com/reforms/ts-jenum
- Owner: reforms
- License: mit
- Created: 2018-08-10T06:40:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-08T13:56:47.000Z (over 2 years ago)
- Last Synced: 2025-10-06T14:21:42.079Z (4 months ago)
- Topics: enum, enum-like-java, java, jenum, tools, ts, typescript, typescript-enum
- Language: TypeScript
- Size: 149 KB
- Stars: 50
- Watchers: 1
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-jenum
* TypeScript Enum like java.lang.Enum
* EnumType
* Enum
* Powerful tool to comfortable work with plain json struct like enum
* EnumTools
# Installation
npm i ts-jenum
# Example in TypeScript
## TypeScript Enum like java.lang.Enum
```typescript
import {Enum, EnumType} from "ts-jenum";
@Enum("text")
export class State extends EnumType() {
static readonly NEW = new State(1, "New");
static readonly ACTIVE = new State(2, "Active");
static readonly BLOCKED = new State(3, "Blocked");
private constructor(readonly code: number, readonly text: string) {
super();
}
}
// Usage example
console.log("" + State.ACTIVE); // "Active"
console.log("" + State.BLOCKED); // "Blocked"
console.log(State.values()); // [State.NEW, State.ACTIVE, State.BLOCKED]
console.log(State.valueOf("New")); // State.NEW
State.valueOf("Unknown") // throw Error(...)
console.log(State.valueByName("NEW")); // State.NEW
console.log(State.ACTIVE.enumName); // ACTIVE
const first = state => state.code === 1;
console.log(State.find("New")); // State.NEW
console.log(State.find(first)); // State.NEW
console.log(State.find("Unknown")); // null
const last = state => state.code === 3;
console.log(State.filter(last)) // [State.BLOCKED]
console.log(State.keys()) // ["NEW", "ACTIVE", "BLOCKED"]
// be "NEW" | "ACTIVE" | "BLOCKED"
type StateNameUnion = EnumConstNames;
```
## EnumTools powerful tool to comfortable work with plain json struct like enum
```typescript
import {EnumTools} from "ts-jenum";
// plain json like enum
const Colors = {
WHITE: "#FFFFFF",
GRAY: "#808080",
BLACK: "#000000"
};
// to be ["WHITE", "GRAY", "BLACK"]
const keys = EnumTools.keys(Colors);
// to be ["#FFFFFF", "#808080", "#000000"]
const values = EnumTools.values(Colors);
/**
* to be {
* "#FFFFFF": "WHITE",
* "#808080": "GRAY",
* "#000000": "BLACK"
* };
*/
const rStruct = EnumTools.reverse(Colors);
/**
* to be: [
* {key: "WHITE", value: "#FFFFFF"},
* {key: "GRAY", value: "#808080"},
* {key: "BLACK", value: "#000000"}
* ]
*/
const pairs = EnumTools.pairs(Colors);
/**
* To be class like:
* @Enum("key")
* class ColorEnum extends EnumType() {
* static readonly WHITE = new ColorEnum("WHITE", "#FFFFFF");
* static readonly GRAY = new ColorEnum("GRAY", "#808080");
* static readonly BLACK = new ColorEnum("BLACK", "#000000");
* private constructor(readonly key: string, readonly value: string | number) {
* super();
* }
* }
* ColorEnum has all IDE hint for developer, type checking and type safety
*/
const ColorEnum = EnumTools.toClass(Colors);
```
Details. Type safety.
In example above, you can write "tExt" or "txt" instead of "text" as @Enum decorator argument and no exception happen. In example below this problem is absent. Add an expression <State> to @Enum decorator
```typescript
import {Enum, EnumConstNames, EnumType} from "ts-jenum";
@Enum("text")
export class State extends EnumType() {
static readonly NEW = new State(1, "New");
static readonly ACTIVE = new State(2, "Active");
static readonly BLOCKED = new State(3, "Blocked");
private constructor(readonly code: number, readonly text: string) {
super();
}
}
// Get Enum Names
// be "NEW" | "ACTIVE" | "BLOCKED"
type StateNameUnion = EnumConstNames;
```
Powerful typing.
```typescript
@Enum("id")
class Person extends EnumType() {
static readonly IVAN = new Person(1 as const, "Ivan" as const);
static readonly JOHN = new Person(2 as const, "John" as const);
private constructor(readonly id: IdType, readonly name: NameType) {
super();
}
static doSomeWork(): void {
// type to be: "Ivan". Not string!
const name = Person.IVAN.name;
// to be: error
// if (name === "cat")
// ^ This condition will always return 'false' since the types '"Ivan"' and '"cat"' have no overlap.
// type to be: 1. Not number!
const id = Person.IVAN.id;
// to be: error
// if (id === 3)
// ^ This condition will always return 'false' since the types '1' and '3' have no overlap
}
}
```