Type
Index
Interfaces
Module
Type parameters
- T
typeName
inherited__inspect__
When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)
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 }'
inherited__decode__
Returns the decoded
input,Result.OkorResult.Error()interface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const input: unknown = ...;const decoded = Codec.decode(someCodec, input);
inherited__encode__
Returns the encoded
inputinterface 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
const someCodec: Codec<unknown> = ...;const jsonSchema = Codec.schema(someCodec);
inheritedasInstance
Try to convert anyValue to enum value or else returns
Option.Noneconst StringType: Type<string>;StringType.asInstance('foo'); // Option.Some('foo')StringType.asInstance(12); // Option.None
inheritedasString
Converts the given value to a String.
Number.asString(123); // '123'
inheritedhasInstance
Return
trueif the given value is an instance of the class.const StringType: Type<string>;StringType.hasInstance('foo'); // trueStringType.hasInstance(42); // false
Parameters
Type module constructor parameters
Type parameters
- T
optionalinherited__inspect__
When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)
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 }'
hasInstance
inheritedtypeName
Type string representation
StringType.typeName // 'String'
Int.typeName // 'Int'
Person.typeName // 'Person'
optionalinherited__decode__
Returns the decoded
input,Result.OkorResult.Error()interface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const input: unknown = ...;const decoded = Codec.decode(someCodec, input);
optionalinherited__encode__
Returns the encoded
inputinterface 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
const someCodec: Codec<unknown> = ...;const jsonSchema = Codec.schema(someCodec);
optionalinheritedasInstance
Try to convert anyValue to enum value or else returns
Option.Noneconst StringType: Type<string>;StringType.asInstance('foo'); // Option.Some('foo')StringType.asInstance(12); // Option.None
optionalinheritedasString
Converts the given value to a String.
Number.asString(123); // '123'
Type Aliases
TypeOf
Extract the type of object from its module
Type parameters
- V
Variables
bigint
boolean
number
Ordering
RegExp
string
unknown
Functions
Array
Returns a codec for
Array<V>.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')])
Char
Convert an underlying type to a tagged type Alias to
wrap(value)
__call__
__decode__
__encode__
__inspect__
__schema__
asInstance
asString
hasInstance
typeName
unwrap
wrap
constant
A type for constant
value. An encoded value can be specified as second argument.const constantType = Type.constant('_'); // Encoded and decoded value are '_'const someSymbol = Symbol('someSymbol');const someSymbolType = Type.constant(someSymbol, '__someSymbol__'); // Encoded value is '__someSymbol__'
define
Define a new Type module
interface NewType {foo: boolean;}const NewType = Type.define<NewType>({typeName: 'NewType',hasInstance(value) {return typeof value.foo === 'boolean';},});
ensure
Ensure that
valueis a validT. Throw a TypeError otherwise.Type.ensure(Type.String, 'foo'); // voidType.ensure(Type.String, 42); // throw new Error('42 is not a valid String')
Int
Convert an underlying type to a tagged type Alias to
wrap(value)
__call__
__decode__
__encode__
__inspect__
__schema__
asInstance
asString
hasInstance
typeName
unwrap
wrap
Object
Returns a new Type for
P.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') })
Option
Return a new optional type from
Valueconst OptionString = Type.Option(Type.String);
Record
Tuple
union
Return a union of all types
const ABType = Type.anyOf(AType, BType);
URL
Convert an underlying type to a tagged type Alias to
wrap(value)
typeName
__call__
Callable property
__inspect__
When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)
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 }'
__decode__
Returns the decoded
input,Result.OkorResult.Error()interface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const input: unknown = ...;const decoded = Codec.decode(someCodec, input);
__encode__
Returns the encoded
inputinterface 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
const someCodec: Codec<unknown> = ...;const jsonSchema = Codec.schema(someCodec);
asInstance
Try to convert anyValue to enum value or else returns
Option.Noneconst StringType: Type<string>;StringType.asInstance('foo'); // Option.Some('foo')StringType.asInstance(12); // Option.None
asString
Converts the given value to a String.
Number.asString(123); // '123'
hasInstance
Return
trueif the given value is an instance of the class.const StringType: Type<string>;StringType.hasInstance('foo'); // trueStringType.hasInstance(42); // false
unwrap
Convert a tagged value to the underlying type
wrap
Convert an underlying type to a tagged type
UUID
Convert an underlying type to a tagged type Alias to
wrap(value)
typeName
__call__
Callable property
__inspect__
When defined, returns a custom string representation. To be useful, it should be bound to a prototype (ex: Struct)
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 }'
__decode__
Returns the decoded
input,Result.OkorResult.Error()interface SomeObject {foo: string}const someCodec: Codec<SomeObject> = ...;const input: unknown = ...;const decoded = Codec.decode(someCodec, input);
__encode__
Returns the encoded
inputinterface 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
const someCodec: Codec<unknown> = ...;const jsonSchema = Codec.schema(someCodec);
asInstance
Try to convert anyValue to enum value or else returns
Option.Noneconst StringType: Type<string>;StringType.asInstance('foo'); // Option.Some('foo')StringType.asInstance(12); // Option.None
asString
Converts the given value to a String.
Number.asString(123); // '123'
hasInstance
Return
trueif the given value is an instance of the class.const StringType: Type<string>;StringType.hasInstance('foo'); // trueStringType.hasInstance(42); // false
unwrap
Convert a tagged value to the underlying type
wrap
Convert an underlying type to a tagged type
Type module interface