Struct
Index
Constructor
Module
inherited__call__
Callable property
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 }'
readonlytypeName
The factory type constant
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'
create
Construct a new model
inheritedhasInstance
Return
trueif the given value is an instance of the class.const StringType: Type<string>;StringType.hasInstance('foo'); // trueStringType.hasInstance(42); // false
Other
DefineParameters
Type parameters
- Model: Struct<{ _: string }>
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 }'
optionalinheritedhasInstance
typeName
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'
ModuleParameter
Type parameters
- T
Parameters
Extract all parameters to create a new Struct
Type parameters
- Model
type
type
The type property discriminator
create
Return a new Struct from
properties. Struct adds debugging / inspecting abilitiesconst SomeType = Type.define<{ some: boolean }>({ typeName: 'SomeType' });Struct.create(SomeType, { some: true });// Struct { _: 'SomeType', some: true }
define
Return a new
Structdefault factory See Module for additional properties added to the constructortype Model = Struct<{ [Struct.type]: 'Model', foo: boolean }>const Model = Struct.define<Model>({ typeName: 'Model' });const instance = Model({ foo: true }); // { _: 'Model', foo: true }Model.typeName === 'Model' // trueModel.hasInstance(instance); // true
Construct a new model