Field

View source

Describes one typed field in structured reflection metadata.

A field associates the registered TypeId for a value with whether the field may be omitted. Its serial form is always an object with type and optional properties.

Examples

const required = new Field('position')
const optional = new Field('velocity', true)

required.serialize() // { type: 'position', optional: false }
optional.serialize() // { type: 'velocity', optional: true }

Constructor


constructor(type: TypeId, optional?: boolean)

View source

Creates a field descriptor.

Parameters

NameDescriptionDefault
type——
optional?

Whether the field may be omitted.

false

Properties


optional: boolean

View source

Whether this field may be omitted. The constructor defaults this to false, so fields are required unless explicitly marked optional.

Methods


serialize(): FieldSerial

View source

Serializes this descriptor as its current type and optional values. No validation or copying of the type ID is performed.

staticserialize(value: Field): FieldSerial

View source

Converts a field descriptor to a plain serial object.

The returned object contains exactly { type: value.type, optional: value.optional }.

Parameters

NameDescriptionDefault
value——

staticdeserialize(value: FieldSerial, out?: Field): Field

View source

Deserializes a field serial into a Field instance.

When out is omitted, a new Field is created with the serial’s values. When out is supplied, its type and optional properties are replaced in place and the same instance is returned. This method assigns the supplied properties directly and does not call Field.validateSerial.

Parameters

NameDescriptionDefault
value——
out?—...

staticvalidateSerial(value: unknown): value is FieldSerial

View source

Checks whether a value has the serialized field descriptor shape.

Returns true only for a non-null, non-array object with type and optional properties whose values are a string and boolean respectively. Extra properties are allowed; this method does not create a Field.

Parameters

NameDescriptionDefault
value——