Skip to main content

Enum

Index

Interfaces

Type Aliases

Variables

Functions

Interfaces

Module

Module<T>:

Module containing methods for working with enum types


Type parameters

  • T: Record<string, any> = Record<string, unknown>

typeName

Re-exports typeName

readonlyinherited__enumKeys__

__enumKeys__: readonly keyof T[]

An array of all keys

inherited__inspect__

__inspect__: Option<(anyValue, depth, options, inspect) => string>

When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)

@example
import { inspect } from 'node:util';

interface Foo {
foo: boolean;
}
const Foo = Struct.define<Foo>({
typeName: 'Foo',
__inspect__: (self) => `Foo { ${String(self.foo)} }`,
});
const myStruct = Struct.create(Foo, { foo: true });// 'Foo { true }'
inspect(myStruct);// 'Foo { true }'
@param

inheritedindexType

indexType: number

Index type

inherited__decode__

  • Returns the decoded input, Result.Ok or Result.Error()

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const input: unknown = ...;
    const decoded = Codec.decode(someCodec, input);

inherited__encode__

  • __encode__(this, input): unknown
  • Returns the encoded input

    @example
    interface SomeObject {
    foo: string
    }
    const someCodec: Codec<SomeObject> = ...;
    const someObject: SomeObject = { foo: "bar" }
    const encoded = Codec.decode(someCodec, someObject);

inherited__schema__

  • Returns the JSONSchema corresponding to the decoded type

    @example
    const someCodec: Codec<unknown> = ...;
    const jsonSchema = Codec.schema(someCodec);

inheritedasInstance

  • asInstance(this, anyValue): Option<T[keyof T]>
  • Try to convert anyValue to enum value or else returns Option.None

    @example
    const StringType: Type<string>;
    StringType.asInstance('foo'); // Option.Some('foo')
    StringType.asInstance(12); // Option.None

inheritedasString

  • asString(this, self): string
  • Converts the given value to a String.

    @example
    Number.asString(123); // '123'

inheritedat

  • Returns the value at the index

inheritedhasInstance

  • hasInstance(this, anyValue): anyValue is T[keyof T]
  • Return true if the given value is an instance of the class.

    @example
    const StringType: Type<string>;
    StringType.hasInstance('foo'); // true
    StringType.hasInstance(42); // false

inheritedindexOf

  • indexOf(value): Option<number>
  • Returns the integer index of a value

inheritedrange

  • range(start, end): Range<T[keyof T]>
  • Returns an Iterable starting from start to end. If start or end is not in range then returns an empty iterable.

inheritedrangeSize

  • rangeSize(start, end): number
  • Returns the size of a range. If start or end is not in range then returns 0.

Type Aliases

KeyOf

KeyOf<T>: ArrayValue<T[typeof Symbol.enumKeys]>

Return enum keys of T


Type parameters

  • T: Enum<Record<string, any>>

ValueOf

ValueOf<T>: T[KeyOf<T>]

Return enum values of T


Type parameters

  • T: Enum<Record<string, any>>

Variables

enumKeys

enumKeys: __enumKeys__ = Symbol.enumKeys

Symbol for the property holding enum keys

Functions

define

  • define<T>(enumObject): Enum<T>
  • Define a new Enum Object

    @example
    const MyEnum = Enum.define({
    // typeName: 'MyEnum', // Add this we want a named Enum
    Foo: 'foo',
    Bar: 'bar',
    });

keys

  • Returns an array of enum keys

    @example
    const MyEnum = Enum.define({ Foo: 'foo', Bar: 'bar' });
    Enum.keys(MyEnum) // ['Foo', 'Bar']

values

  • Returns an array of enum values

    @example
    const MyEnum = Enum.define({ Foo: 'foo', Bar: 'bar' });
    Enum.values(MyEnum) // ['foo', 'bar']