Skip to main content

Tag

Index

Interfaces

Functions

Interfaces

Module

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

typeName

Re-exports typeName

inherited__call__

__call__: (value) => To

Callable property

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

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

inheritedasString

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

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

inheritedhasInstance

  • hasInstance(this, anyValue): 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): From
  • Convert a tagged value to the underlying type

wrap

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

Parameters

Parameters<T>:

Type module constructor parameters


Type parameters

  • T

optionalinherited__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

inheritedhasInstance

hasInstance: (value) => boolean

inheritedtypeName

typeName: string

Type string representation

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

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

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

optionalinherited__schema__

  • Returns the JSONSchema corresponding to the decoded type

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

optionalinheritedasInstance

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

optionalinheritedasString

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

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

Functions

define

  • define<From, To>(parameters): Tag.Module<From, To>
  • Returns a new Tag module

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