Aller au contenu principal

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

readonly[enumKeys]

[enumKeys]: readonly keyof T[]

An array of all keys

__inspect__

__inspect__: Option<(anyValue: T[keyof T], depth: number, options: InspectOptions, inspect: InspectFunction) => 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

indexType

indexType: number

Index type

__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);

__encode__

  • __encode__(this: void, input: T[keyof T]): 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);

__schema__

  • Returns the JSONSchema corresponding to the decoded type

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

asInstance

  • asInstance(anyValue: unknown): 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

asString

  • asString(this: void, self: T[keyof T]): string
  • Converts the given value to a String.

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

at

  • at(index: number): Option<T[keyof T]>
  • Returns the value at the index

hasInstance

  • hasInstance(anyValue: unknown): 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

indexOf

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

range

  • range(start: T[keyof T], end: T[keyof T]): 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.

rangeSize

  • rangeSize(start: T[keyof T], end: T[keyof T]): 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: symbol = Symbol.enumKeys

Symbol for the property holding enum keys

Functions

define

  • define<T>(enumObject: T & { typeName?: string }): 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',
    });

    Type parameters

    • T: Record<string, string | number | boolean>

keys

  • keys<T>(enumObject: T): ReadonlyArray<Enum.KeyOf<T>>
  • Returns an array of enum keys

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

    Type parameters

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

values

  • Returns an array of enum values

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

    Type parameters

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