# encodeKeyTuple

> **encodeKeyTuple**(`parts`): `Uint8Array`

Defined in: [packages/key-encoding/src/tuple.ts:219](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/key-encoding/src/tuple.ts#L219)

Encode a composite key so that `compareBytes` orders encoded tuples lexicographically by part, each part in its own direction: the first part decides, the second breaks ties, and a tuple sorts before every longer tuple it is a prefix of.

Each part is a one-byte tag naming its kind and direction, then its payload: the fixed-width scalar encoding, or the escaped encoding for `bytes` and `string`. A descending part writes the bitwise complement of its ascending payload; because every payload is self-delimiting, that reverses its order exactly, including between byte strings of different lengths. Two tuples compare meaningfully only part by part over the same kinds, as in one index schema.

Runs in O(total payload bytes) and allocates the result once.

## Parameters

### parts

readonly [`KeyTuplePart`](/reference/knotkit/key-encoding/type-aliases/keytuplepart/)\[]

## Returns

`Uint8Array`

## Throws

`INVALID_KEY_VALUE` when `parts` is not an array, a part’s `kind` or `direction` is unknown, or its `value` is outside the kind’s domain, as the matching `encode*Key` would report.
