Skip to main content

Type

Index

Interfaces

Module

Module<T>:

Type module interface


Type parameters

  • T

typeName

Re-exports typeName

__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

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

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

hasInstance

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

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'

Type Aliases

TypeOf

TypeOf<V>: V extends Type<infer T> ? T : never

Extract the type of object from its module


Type parameters

  • V

Variables

Ordering

Ordering: Enum<{ Equal: 0; Greater: 1; Less: -1; typeName: Ordering }>

RegExp

RegExp: { typeName: any; __inspect__: Option<(anyValue: RegExp, depth: number, options: InspectOptions, inspect: InspectFunction) => string>; parse: (expression: string) => Option<RegExp>; __decode__: any; __encode__: any; __schema__: any; asInstance: any; asString: any; hasInstance: any }

bigint

bigint: Module<bigint>

boolean

boolean: Module<boolean>

number

number: Module<number>

string

string: Module<string>

unknown

unknown: Module<unknown>

Functions

Array

  • Returns a codec for Array<V>.

    @example
    const codec = Type.Array(dateISO);
    const encoded = Codec.encode(codec, [new Date('1970-01-01T00:00:00.000Z')]);// ['1970-01-01T00:00:00.000Z']
    const decoded = Codec.decode(codec, ['1970-01-01T00:00:00.000Z']);// Result.Ok([Date('1970-01-01T00:00:00.000Z')])

    Type parameters

    • V

Char

  • Char(value: string): Char
  • Convert an underlying type to a tagged type Alias to wrap(value)

__call__

Re-exports __call__

__decode__

Re-exports __decode__

__encode__

Re-exports __encode__

__inspect__

Re-exports __inspect__

__schema__

Re-exports __schema__

asInstance

Re-exports asInstance

asString

Re-exports asString

hasInstance

Re-exports hasInstance

typeName

Re-exports typeName

unwrap

Re-exports unwrap

wrap

Re-exports wrap

Int

  • Int(value: number): Int
  • Convert an underlying type to a tagged type Alias to wrap(value)

__call__

Re-exports __call__

__decode__

Re-exports __decode__

__encode__

Re-exports __encode__

__inspect__

Re-exports __inspect__

__schema__

Re-exports __schema__

asInstance

Re-exports asInstance

asString

Re-exports asString

hasInstance

Re-exports hasInstance

typeName

Re-exports typeName

unwrap

Re-exports unwrap

wrap

Re-exports wrap

Object

  • Object<P>(Properties: { readonly [ K in string | number | symbol ]: Module<P[K]> }, typeName?: string): Type.Module<Readonly<P>>
  • Returns a new Type for P.

    @example
    const SomeType = Type.Object({ created: dateISO }, 'SomeType');
    const encoded = Codec.encode(SomeType, { created: new Date('1970-01-01T00:00:00.000Z') });// { created: '1970-01-01T00:00:00.000Z' }
    const decoded = Codec.decode(SomeType, { created: '1970-01-01T00:00:00.000Z' });// Result.Ok({ created: Date('1970-01-01T00:00:00.000Z') })

    Type parameters

    • P

Option

  • Return a new optional type from Value

    @example
    const OptionString = Type.Option(Type.String);

    Type parameters

    • T

Record

Tuple

  • Type parameters

URL

  • URL(value: string): URL
  • Convert an underlying type to a tagged type Alias to wrap(value)

typeName

Re-exports typeName

__call__

__call__: (value: string) => URL

Callable property

__inspect__

__inspect__: Option<(anyValue: URL, 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: URL): 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<URL>
  • 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: URL): string
  • Converts the given value to a String.

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

hasInstance

  • hasInstance(anyValue: unknown): anyValue is URL
  • 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: URL): string
  • Convert a tagged value to the underlying type

wrap

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

UUID

  • UUID(value: `${string}-${string}-${string}-${string}-${string}`): UUID
  • Convert an underlying type to a tagged type Alias to wrap(value)

typeName

Re-exports typeName

__call__

__call__: (value: `${string}-${string}-${string}-${string}-${string}`) => UUID

Callable property

__inspect__

__inspect__: Option<(anyValue: UUID, 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: UUID): 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<UUID>
  • 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: UUID): string
  • Converts the given value to a String.

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

hasInstance

  • hasInstance(anyValue: unknown): anyValue is UUID
  • 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: UUID): `${string}-${string}-${string}-${string}-${string}`
  • Convert a tagged value to the underlying type

wrap

  • wrap(value: `${string}-${string}-${string}-${string}-${string}`): UUID
  • Convert an underlying type to a tagged type

constant

  • constant<Value>(value: Value, encodedValue?: JSONPrimitive): Type.Module<Value>
  • constant<Value>(value: Value, encodedValue: JSONPrimitive): Type.Module<Value>
  • A type for constant value. An encoded value can be specified as second argument.

    @example
    const constantType = Type.constant('_'); // Encoded and decoded value are '_'
    @example
    const someSymbol = Symbol('someSymbol');
    const someSymbolType = Type.constant(someSymbol, '__someSymbol__'); // Encoded value is '__someSymbol__'

    Type parameters

    • Value: null | string | number | boolean

define

  • Define a new Type module

    @example
    interface NewType {
    foo: boolean;
    }
    const NewType = Type.define<NewType>({
    typeName: 'NewType',
    hasInstance(value) {
    return typeof value.foo === 'boolean';
    },
    });

    Type parameters

    • T

ensure

  • ensure<T>(Type: Type<T>, anyValue: unknown): asserts anyValue is T
  • Ensure that value is a valid T. Throw a TypeError otherwise.

    @example
    Type.ensure(Type.String, 'foo'); // void
    Type.ensure(Type.String, 42); // throw new Error('42 is not a valid String')

    Type parameters

    • T

union

  • Return a union of all types

    @example
    const ABType = Type.anyOf(AType, BType);

    Type parameters