Aller au contenu principal

Enumerable <T>

Type module interface

Hierarchy

Index

Codec

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

Formatting

asString

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

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

Indexable

indexType

indexType: number

Index type

at

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

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

__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

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

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

Other

typeName

Re-exports typeName

readonly[enumKeys]

[enumKeys]: readonly keyof T[]

An array of all keys