Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "5.23.0",
"version": "5.24.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
20 changes: 20 additions & 0 deletions src/globals/Enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @athenna/common
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { Enum as EnumImpl } from '#src/helpers/Enum'

declare global {
export class Enum extends EnumImpl {}
}

const __global: any = global

if (!__global.Enum) {
__global.Enum = EnumImpl
}
13 changes: 12 additions & 1 deletion src/helpers/Enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import type { InferEnum } from '#src/types'
import { Macroable } from '#src/helpers/Macroable'

export class Enum extends Macroable {
Expand Down Expand Up @@ -35,7 +36,7 @@ export class Enum extends Macroable {
* ```ts
* export class StatusEnum extends Enum {
* public static PENDING = 'pending' as const
* public static static APPROVED = 'approved' as const
* public static APPROVED = 'approved' as const
* public static BLOCKED = 'blocked' as const
* }
*
Expand All @@ -62,3 +63,13 @@ export class Enum extends Macroable {
return this.keys().map(key => [key, this[key]])
}
}

// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Enum {
/**
* Infer the type of your enum values. Useful to be used
* in models and other types to define that that property
* should only expect the values defined by your enum.
*/
export type infer<T> = InferEnum<Omit<T, 'infer'>>
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from '#src/types'

export * from '#src/constants/alphabet'

export * from '#src/globals/Enum'
export * from '#src/globals/Error'
export * from '#src/globals/Array'

Expand Down
12 changes: 12 additions & 0 deletions src/types/InferEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @athenna/common
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

export type InferEnum<T> = {
[K in keyof T]: T[K] extends string | number ? T[K] : never
}[keyof T]
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type {
export * from '#src/types/Merge'
export * from '#src/types/Except'
export * from '#src/types/PathDirs'
export * from '#src/types/InferEnum'
export * from '#src/types/CommandInput'
export * from '#src/types/CommandOutput'
export * from '#src/types/NodeCommandInput'
Expand Down