diff --git a/package-lock.json b/package-lock.json index 9c70532..45dcb7e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "5.23.0", + "version": "5.24.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "5.23.0", + "version": "5.24.0", "license": "MIT", "dependencies": { "@fastify/formbody": "^8.0.2", diff --git a/package.json b/package.json index b103f19..d42f2b0 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/globals/Enum.ts b/src/globals/Enum.ts new file mode 100644 index 0000000..6d5cc13 --- /dev/null +++ b/src/globals/Enum.ts @@ -0,0 +1,20 @@ +/** + * @athenna/common + * + * (c) João Lenon + * + * 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 +} diff --git a/src/helpers/Enum.ts b/src/helpers/Enum.ts index 71b9555..b282dd1 100644 --- a/src/helpers/Enum.ts +++ b/src/helpers/Enum.ts @@ -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 { @@ -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 * } * @@ -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 = InferEnum> +} diff --git a/src/index.ts b/src/index.ts index 4c6311e..31e72c9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/types/InferEnum.ts b/src/types/InferEnum.ts new file mode 100644 index 0000000..635e6fc --- /dev/null +++ b/src/types/InferEnum.ts @@ -0,0 +1,12 @@ +/** + * @athenna/common + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +export type InferEnum = { + [K in keyof T]: T[K] extends string | number ? T[K] : never +}[keyof T] diff --git a/src/types/index.ts b/src/types/index.ts index 8fd4e73..6f1be16 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -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'