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

@example

@param

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

codecDecode

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

codecEncode

  • codecEncode(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);

codecSchema

  • Returns the JSONSchema corresponding to the decoded type

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

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

optionalasInstance

asInstance?: (anyValue: unknown) => Option<T>

hasInstance

hasInstance: (value: unknown) => boolean

optionalinspect

inspect?: Option<(anyValue: T, depth: number, options: InspectOptions, inspect: InspectFunction) => string>

typeName

typeName: string

optionalcodecDecode

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

optionalcodecEncode

  • codecEncode(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);

optionalcodecSchema

  • Returns the JSONSchema corresponding to the decoded type

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

Type Aliases

TypeOf

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

Extract the type of object from its module


Type parameters

  • V

Variables

RegExp

RegExp: { typeName: any; inspect: Option<(anyValue: RegExp, depth: number, options: InspectOptions, inspect: InspectFunction) => string>; parse: (expression: string) => Option<RegExp>; asInstance: any; codecDecode: any; codecEncode: any; codecSchema: 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__

asInstance

Re-exports asInstance

codecDecode

Re-exports codecDecode

codecEncode

Re-exports codecEncode

codecSchema

Re-exports codecSchema

hasInstance

Re-exports hasInstance

inspect

Re-exports inspect

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__

asInstance

Re-exports asInstance

codecDecode

Re-exports codecDecode

codecEncode

Re-exports codecEncode

codecSchema

Re-exports codecSchema

hasInstance

Re-exports hasInstance

inspect

Re-exports inspect

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

@example

@param

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

codecDecode

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

codecEncode

  • codecEncode(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);

codecSchema

  • Returns the JSONSchema corresponding to the decoded type

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

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

@example

@param

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

codecDecode

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

codecEncode

  • codecEncode(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);

codecSchema

  • Returns the JSONSchema corresponding to the decoded type

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

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