Toolbox-XToolbox-X

Convert Object Values

Converts specified values in objects or arrays of objects between string and number types, supporting nested properties via dot notation.

convertObjectValues

The convertObjectValues function converts specified values in objects or arrays of objects between string and number types. It supports nested properties via dot notation.

Function Signatures

// For Single Object
function convertObjectValues<
  T extends GenericObject,
  Key extends NumericDotKey<T>,
  C extends 'string' | 'number',
>(data: T, options: ConvertObjectOptions<T, Key, C>): ConvertedObject<T, Key, C>

// For Array of Objects
function convertObjectValues<
  T extends GenericObject,
  Key extends NumericDotKey<T>,
  C extends 'string' | 'number',
>(data: Array<T>, options: ConvertObjectOptions<T, Key, C>): Array<ConvertedObject<T, Key, C>>

Parameters

  • data (T | T[]): A single object or an array of objects.
  • options (ConvertObjectOptions): The options for conversion:
    • keys (Key[]): An array of dot-notation keys (properties) to convert.
    • convertTo ('string' | 'number'): The target type to convert selected keys to.

Return Value

  • A new object or array of objects with the specified keys converted to the target type, with accurate TypeScript return types.

Example Usage

playground.ts

Types

ConvertObjectOptions

interface ConvertObjectOptions<
  T extends GenericObject,
  Key extends NumericDotKey<T>,
  C extends 'string' | 'number'
> {
  keys: ValidArray<Key>;
  convertTo: C;
}

Last updated: Sun, Jun 14, 2026 07:06:16AM (UTC)

On this page