Skip to main content

Enums

Oidis uses a class-based enum pattern built on BaseEnum from io-oidis-commons. This gives you type-safe constants plus runtime introspection — something native TypeScript enum cannot provide.

Package: io-oidis-commons

import { BaseEnum } from "@io-oidis-commons/Io/Oidis/Commons/Primitives/BaseEnum.js";

Creating an Enum​

Extend BaseEnum and declare values as public static readonly string constants:

import { BaseEnum } from "@io-oidis-commons/Io/Oidis/Commons/Primitives/BaseEnum.js";

export class EANType extends BaseEnum {
public static readonly PRODUCTION : string = "D";
public static readonly CONSUMPTION : string = "O";
}

Naming Conventions​

ElementConventionExample
Class namePascalCase, descriptiveEANType, RecordStatus, DataFormatType
ConstantsUPPER_SNAKE_CASEPRODUCTION, UTF8, URL_ENCODED
ValuesMeaningful short strings"D", "utf8", "active"

File Placement​

Place enum classes in the Models/ directory alongside other domain types. Enums are not @Entity models — they are standalone classes.

Usage​

Direct Comparison​

if (summary.Type === EANType.PRODUCTION) {
// handle production EAN
}

Runtime Reflection​

BaseEnum provides static methods for runtime introspection via the Oidis Reflection system:

MethodReturnsDescription
Contains($value)booleanCheck if a value belongs to the enum
getKey($value)stringReverse-lookup: value → constant name
getProperties()string[]List all constant names
ClassName()stringFully qualified class name
EANType.Contains("D");          // true
EANType.Contains("X"); // false
EANType.getKey("D"); // "PRODUCTION"
EANType.getProperties(); // ["PRODUCTION", "CONSUMPTION"]

Interface Type Annotations​

When an interface field holds an enum value, type it as string — not as a union literal. BaseEnum values are string at the type level:

// ✅ Correct — works with BaseEnum
export interface IEDCReportEAN {
ean : string;
type : string;
}

// ❌ Wrong — hardcoded union, cannot use BaseEnum
export interface IEDCReportEAN {
ean : string;
type : "D" | "O";
}

Model Integration​

Enums work naturally with @Property on model fields:

@Property({ type: RecordStatus, default: RecordStatus.ACTIVE })
public declare Status : RecordStatus;

The framework serializes/deserializes the string value. See Entity & Property for details.

BaseEnum vs TypeScript enum​

FeatureBaseEnumTypeScript enum
Runtime reflection✅ Contains, getKey, getProperties❌ None
Oidis Reflection integration✅ Discovered by namespace scanner❌ Not a class
@Property type support✅ Works as model field type❌ Not supported
Tree-shakeable✅ Standard class⚠️ Generates extra JS code
Exhaustiveness checking❌ Not enforced by TS compiler✅ switch exhaustiveness

Rule: Always use BaseEnum in Oidis projects. TypeScript native enum does not integrate with the framework's reflection and model systems.


See also: Models Overview | Entity and Property | Reflection