API reference / @evolu/common / Type / record

Function: record()

function record<
  KeyName,
  KeyT,
  KeyInput,
  KeyError,
  KeyParent,
  KeyParentError,
  Value,
>(
  keyType,
  valueType,
): RecordType<
  KeyName,
  KeyT,
  KeyInput,
  KeyError,
  KeyParent,
  KeyParentError,
  Value
>;

Defined in: packages/common/src/Type.ts:2549

Record of a key Type and value Type.

  • The input must be a plain object (validated by isPlainObject).
  • Each key is validated/transformed by the key Type.
  • Each value is validated/transformed by the value Type.

The resulting type is Readonly<Record<KeyT, ValueT>>.

Example

const StringToNumberRecord = record(String, Number);

// ok({ "a": 1, "b": 2 })
StringToNumberRecord.from({ a: 1, b: 2 });

// err => "Key" because 42 is not a string key
StringToNumberRecord.from({ 42: 1, b: 2 });

// err => "Value" because "x" is not a number
StringToNumberRecord.from({ a: "x", b: 2 });

Type Parameters

Type Parameter
KeyName extends string
KeyT extends string
KeyInput extends string
KeyError extends TypeError<Capitalize<string>>
KeyParent extends string
KeyParentError extends TypeError<Capitalize<string>>
Value extends AnyType

Parameters

ParameterType
keyTypeType<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError>
valueTypeValue

Returns

RecordType<KeyName, KeyT, KeyInput, KeyError, KeyParent, KeyParentError, Value>

Was this page helpful?