Skip to main content

Struct

Index

Constructor

Module

  • Construct a new model

__call__

__call__: (properties: Parameters<Model>) => Model

Callable property

__inspect__

__inspect__: Option<(anyValue: Model, 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

readonlytypeName

typeName: Model[_]

The factory type constant

__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: Model): 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<Model>
  • 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: Model): string
  • Converts the given value to a String.

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

create

  • create(this: void, properties: Parameters<Model>): Model
  • Construct a new model

hasInstance

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

DefineParameters

DefineParameters<Model>:

Type parameters

optional__inspect__

__inspect__?: Option<(anyValue: Model, 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

optionalhasInstance

hasInstance?: (value: unknown) => boolean

typeName

typeName: Model[_]

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

optional__encode__

  • __encode__(this: void, input: Model): 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);

optional__schema__

  • Returns the JSONSchema corresponding to the decoded type

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

optionalasInstance

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

optionalasString

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

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

Parameters

Parameters<Model>: Omit<Model, Struct.type>

Extract all parameters to create a new Struct


Type parameters

  • Model

type

type: typeof type

consttype

type: _ = '_'

The type property discriminator

create

  • create<Properties>(module: Type<Properties> & AsString<Properties>, properties: Properties): Properties
  • Return a new Struct from properties. Struct adds debugging / inspecting abilities

    @example
    const SomeType = Type.define<{ some: boolean }>({ typeName: 'SomeType' });

    Struct.create(SomeType, { some: true });// Struct { _: 'SomeType', some: true }

    Type parameters

    • Properties

define

  • Return a new Struct default factory See Module for additional properties added to the constructor

    @example
    type Model = Struct<{ [Struct.type]: 'Model', foo: boolean }>
    const Model = Struct.define<Model>({ typeName: 'Model' });

    const instance = Model({ foo: true }); // { _: 'Model', foo: true }
    Model.typeName === 'Model' // true
    Model.hasInstance(instance); // true

    Type parameters

    • Model: Readonly<{ _: string }>