Skip to main content

Try it Live

Run RLP examples in the interactive playground

RLP Types

RLP data type system defining bytes data, list data, and type guards for working with RLP structures.

Overview

RLP uses a discriminated union type to represent two kinds of data:
  • BytesData - Leaf nodes containing raw bytes
  • ListData - Nested arrays of RLP data
Type guards provide runtime type checking and TypeScript type narrowing for safe data access.

BrandedRlp

Core type representing RLP data structures.
Branded type using discriminated union pattern. The type field distinguishes between bytes and list variants. Properties:
  • type: "bytes" | "list" - Discriminant field
  • value: Uint8Array | BrandedRlp[] - Data payload
Source: BrandedRlp.ts:4-6

BytesData Variant

Represents a leaf node with raw bytes:
Examples:

ListData Variant

Represents a nested array of RLP data:
Examples:

Type Guards

Runtime type checking functions that narrow TypeScript types.

isData

Checks if value is any RLP data structure.

    Implementation

    Checks for required structure:
    1. Value is object (not null)
    2. Has type field
    3. Has value field
    4. Type is either “bytes” or “list”

    isBytesData

    Checks if value is bytes data specifically.

      Implementation

      Uses isData then checks type field:

      isListData

      Checks if value is list data specifically.

        Implementation

        Uses isData then checks type field:

        Type Patterns

        Discriminated Union Pattern

        RLP uses TypeScript discriminated unions for type safety:

        Type Narrowing with Guards

        Combine type guards with TypeScript narrowing:

        Exhaustive Type Checking

        Ensure all type variants are handled:

        Working with Types

        Creating RLP Data

        Use Rlp() constructor for automatic type detection:

        Manual Construction

        Create structures directly:

        Type-safe Access

        Use type guards for safe access:

        Type Validation

        Runtime Validation

        Type guards perform runtime validation:

        Type Assertions

        Assert types when you know the structure:

        Encodable

        Input type for encoding operations:
        Accepts raw bytes, RLP data, or nested arrays. See Encoding.

        Decoded

        Output type from decoding operations:
        Contains decoded RLP data and remaining bytes. See Decoding.

        Type Examples

        Transaction Type

        Merkle Proof Type