Skip to main content

Tag

Index

Interfaces

Functions

Interfaces

Module

  • Module(value: From): To
  • Convert an underlying type to a tagged type Alias to wrap(value)

typeName

Re-exports typeName

__call__

__call__: (value: From) => To

Callable property

__inspect__

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

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

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

hasInstance

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

unwrap

  • unwrap(value: To): From
  • Convert a tagged value to the underlying type

wrap

  • wrap(value: From): To
  • Convert an underlying type to a tagged type

Parameters

Parameters<T>:

Type module constructor parameters


Type parameters

  • T

optional__inspect__

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

hasInstance

hasInstance: (value: unknown) => boolean

typeName

typeName: string

Type string representation

@example
StringType.typeName // 'String'
Int.typeName // 'Int'
Person.typeName // 'Person'

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

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<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

optionalasString

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

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

Functions

define

  • Returns a new Tag module

    @example
    type Foo = string & Tag<'Foo'>;
    const Foo = Tag.define<string, Foo>({
    typeName: 'Foo',
    hasInstance: (anyValue) => typeof anyValue === 'string',
    });

    Type parameters

    • From
    • To
Page Options