Skip to main content

MappedType

Package: @opra/common

MappedType is the runtime class that represents a derived object type created by applying field transformations to an existing ComplexType, MixinType, or another MappedType. It is the runtime counterpart of the OmitType, PickType, PartialType, and RequiredType factory functions.

A MappedType shares the full field API of ComplexTypeBase — you can iterate its fields, look up individual fields, and generate codecs, just like with ComplexType.


Inheritance

DocumentElement
└── DataType
└── ComplexTypeBase ← shared field API (fields, findField, getField, …)
└── MappedType

Properties

Properties inherited from DataType:

PropertyTypeDescription
kind'MappedType'Always 'MappedType'.
namestring | undefinedRegistry name. undefined for anonymous mapped types.
descriptionstring | undefinedHuman-readable description.
abstractboolean | undefinedWhether the type is abstract.
embeddedbooleantrue when the type has no name.

Properties inherited from ComplexTypeBase:

PropertyTypeDescription
keyFieldstring | undefinedPrimary key field name (inherited from base if the key field was not omitted).
additionalFieldsboolean | DataType | ['error'] | ['error', string] | undefinedInherited from base (suppressed when pick is set).
ctorType | undefinedTypeScript class constructor (inherited from base).

Properties on MappedType:

PropertyTypeDescription
baseComplexType | MappedType | MixinTypeThe source type this MappedType was derived from.
pickstring[] | undefinedField names retained from base (set when created via PickType).
omitstring[] | undefinedField names removed from base (set when created via OmitType).
partialstring[] | boolean | undefinedFields made optional — array of names, or true for all (set via PartialType).
requiredstring[] | boolean | undefinedFields made required — array of names, or true for all (set via RequiredType).
discriminatorFieldstring | undefinedInherited from base.
discriminatorValuestring | undefinedInherited from base.

Field methods

All field methods are inherited from ComplexTypeBase and behave identically to ComplexType. See ComplexType — Field methods.

MethodDescription
findField(nameOrPath, scope?)Returns the ApiField at the given name/path, or undefined.
getField(nameOrPath, scope?)Same as findField but throws if not found.
fields(scope?)Iterator over all ApiField instances visible in scope.
fieldNames(scope?)Iterator over field names.
fieldEntries(scope?)Iterator over [name, ApiField] pairs.
fieldCount(scope?)Count of fields visible in scope.
parseFieldPath(path, options?)Resolves a dot-separated path to an array of ParsedFieldPath items.
normalizeFieldPath(path, options?)Resolves a path and returns its canonical dot-separated form.

Methods

extendsFrom(baseType)

Returns true if this mapped type's base (or its base chain) extends from the given type.

extendsFrom(baseType: DataType | string | Type | object): boolean
const updateDto = document.node.getDataType('UpdateCustomerDto');
if (updateDto instanceof MappedType) {
updateDto.extendsFrom('Customer'); // true if derived from Customer
}

generateCodec(codec, options?)

Inherited from ComplexTypeBase. Compiles a codec that validates/transforms the fields of this mapped type (respecting pick/omit and partial/required transformations).

generateCodec(
codec: 'encode' | 'decode',
options?: DataType.GenerateCodecOptions
): Validator

toJSON(options?)

Returns an OpraSchema.MappedType plain object for schema export, including base, pick/omit, and partial/required.

toJSON(options?: ApiDocument.ExportOptions): OpraSchema.MappedType

Obtaining a MappedType instance

Mapped types appear in a document when a controller or operation registers a type produced by OmitType, PickType, PartialType, or RequiredType.

import { MappedType } from '@opra/common';

const type = document.node.getDataType('UpdateCustomerDto');

if (type instanceof MappedType) {
console.log(type.kind); // 'MappedType'
console.log(type.base.name); // 'Customer'
console.log(type.omit); // ['_id'] or undefined
console.log(type.partial); // true or string[]
}

ApiDocument · MappedType · ComplexType