TypeScript Type-Preserving Case Converter

2.1.0 · active · verified Tue Apr 21

ts-case-convert is a TypeScript utility library designed to transform object keys between common casing conventions such as camelCase, snake_case, and PascalCase. Its core strength lies in its robust TypeScript type preservation, offering precise type inference for converted objects, thereby maintaining code completion and compile-time validation. This feature is particularly valuable in large TypeScript applications that frequently interact with APIs employing diverse casing styles. The library is currently stable at version `2.1.0` and exhibits an active release cadence with frequent patch and minor updates addressing bug fixes and feature enhancements, indicating ongoing maintenance. It intelligently handles complex nested objects, arrays of objects, and correctly preserves primitive types, null, undefined, Date objects, and Uint8Array instances during conversion, preventing unintended data corruption or type inaccuracies.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates converting a complex object with nested structures, arrays, and special types (Date, null, undefined) from snake_case to camelCase and vice-versa, showcasing accurate type preservation and property access.

import { objectToCamel, objectToSnake } from 'ts-case-convert';

const sourceObject = {
  user_id: 123,
  first_name: 'John',
  last_name: 'Doe',
  email_address: 'john.doe@example.com',
  is_active: true,
  created_at: new Date('2023-01-01T10:00:00Z'),
  addresses_list: [
    { street_name: 'Main St', street_number: 123 },
    { street_name: 'Oak Ave', street_number: 45 },
  ],
  settings_map: {
    theme_color: 'blue',
    font_size: 16,
  },
  null_value: null,
  undefined_value: undefined,
};

// Convert keys to camelCase
const camelCaseObject = objectToCamel(sourceObject);

// Demonstrates type inference and access
type UserIDType = typeof camelCaseObject.userId; // type is 'number'
const userId: UserIDType = camelCaseObject.userId;
console.log('CamelCase Object:', camelCaseObject);
console.log('User ID:', userId); // Access property with camelCase

// Convert keys back to snake_case
const snakeCaseObject = objectToSnake(camelCaseObject);

// Demonstrates type inference and access
type FirstNameType = typeof snakeCaseObject.first_name; // type is 'string'
const firstName: FirstNameType = snakeCaseObject.first_name;
console.log('SnakeCase Object:', snakeCaseObject);
console.log('First Name:', firstName); // Access property with snake_case

// Example of nested access and type preservation
const firstAddressCamel = camelCaseObject.addressesList[0];
type StreetNameCamel = typeof firstAddressCamel.streetName; // type is 'string'
console.log('First address street name (camel):', firstAddressCamel.streetName);

const firstAddressSnake = snakeCaseObject.addresses_list[0];
type StreetNameSnake = typeof firstAddressSnake.street_name; // type is 'string'
console.log('First address street name (snake):', firstAddressSnake.street_name);

view raw JSON →