{"id":15862,"library":"ts-case-convert","title":"TypeScript Type-Preserving Case Converter","description":"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.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/RossWilliams/ts-case-convert","tags":["javascript","PascalCase","camel-case","camelCase","conversion","pascal-case","snake-case","snake_case","typescript"],"install":[{"cmd":"npm install ts-case-convert","lang":"bash","label":"npm"},{"cmd":"yarn add ts-case-convert","lang":"bash","label":"yarn"},{"cmd":"pnpm add ts-case-convert","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ts-case-convert primarily uses ESM; CommonJS `require` should be avoided for new projects.","wrong":"const objectToCamel = require('ts-case-convert').objectToCamel","symbol":"objectToCamel","correct":"import { objectToCamel } from 'ts-case-convert'"},{"note":"ESM is the recommended import style. Use default imports carefully, as this library uses named exports.","wrong":"const objectToSnake = require('ts-case-convert').objectToSnake","symbol":"objectToSnake","correct":"import { objectToSnake } from 'ts-case-convert'"},{"note":"Utility types like `CamelKeys` and `SnakeKeys` can be imported for manual type manipulation, exposed since v2.1.0.","symbol":"CamelKeys","correct":"import type { CamelKeys } from 'ts-case-convert'"}],"quickstart":{"code":"import { objectToCamel, objectToSnake } from 'ts-case-convert';\n\nconst sourceObject = {\n  user_id: 123,\n  first_name: 'John',\n  last_name: 'Doe',\n  email_address: 'john.doe@example.com',\n  is_active: true,\n  created_at: new Date('2023-01-01T10:00:00Z'),\n  addresses_list: [\n    { street_name: 'Main St', street_number: 123 },\n    { street_name: 'Oak Ave', street_number: 45 },\n  ],\n  settings_map: {\n    theme_color: 'blue',\n    font_size: 16,\n  },\n  null_value: null,\n  undefined_value: undefined,\n};\n\n// Convert keys to camelCase\nconst camelCaseObject = objectToCamel(sourceObject);\n\n// Demonstrates type inference and access\ntype UserIDType = typeof camelCaseObject.userId; // type is 'number'\nconst userId: UserIDType = camelCaseObject.userId;\nconsole.log('CamelCase Object:', camelCaseObject);\nconsole.log('User ID:', userId); // Access property with camelCase\n\n// Convert keys back to snake_case\nconst snakeCaseObject = objectToSnake(camelCaseObject);\n\n// Demonstrates type inference and access\ntype FirstNameType = typeof snakeCaseObject.first_name; // type is 'string'\nconst firstName: FirstNameType = snakeCaseObject.first_name;\nconsole.log('SnakeCase Object:', snakeCaseObject);\nconsole.log('First Name:', firstName); // Access property with snake_case\n\n// Example of nested access and type preservation\nconst firstAddressCamel = camelCaseObject.addressesList[0];\ntype StreetNameCamel = typeof firstAddressCamel.streetName; // type is 'string'\nconsole.log('First address street name (camel):', firstAddressCamel.streetName);\n\nconst firstAddressSnake = snakeCaseObject.addresses_list[0];\ntype StreetNameSnake = typeof firstAddressSnake.street_name; // type is 'string'\nconsole.log('First address street name (snake):', firstAddressSnake.street_name);","lang":"typescript","description":"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."},"warnings":[{"fix":"Review existing code that relies on `kebab-case` conversion if upgrading from v1.x. Adjust input keys or expect different output for `kebab-case` strings.","message":"In version 2.0.0, the conversion logic for kebab-case strings changed. Previously, `kebab-case` was treated as a single unit; now, hyphens are considered unit breaks, affecting how it converts to `camelCase` or `PascalCase`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade to `ts-case-convert@2.0.5` or later to ensure `Date` objects are preserved. Upgrade to `ts-case-convert@2.0.2` or later for `Buffer` object preservation.","message":"Prior to versions 2.0.5 and 2.0.2, `Date` objects and Node.js `Buffer` objects were not correctly preserved during conversion, potentially leading to them being converted into generic plain objects or having their internal keys transformed. This could corrupt data or break functionality.","severity":"gotcha","affected_versions":"<2.0.5"},{"fix":"Upgrade to `ts-case-convert@2.0.1` or later to ensure top-level arrays are correctly typed and returned as arrays.","message":"In early 2.x versions (e.g., prior to 2.0.1), passing a top-level array to conversion functions would incorrectly result in an object type instead of an array type being returned, affecting type inference and direct array method calls.","severity":"gotcha","affected_versions":"<2.0.1"},{"fix":"Upgrade to `ts-case-convert@2.0.4` or later, which switched to `Uint8Array` internally to remove Node.js-specific dependencies and improve browser compatibility.","message":"Versions prior to 2.0.4 had hard dependencies on the Node.js `Buffer` global object, causing issues when used in non-Node.js environments (like browsers) that do not define `Buffer`.","severity":"gotcha","affected_versions":"<2.0.4"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"This is expected behavior in v2.0.0+. Adjust your input data or conversion expectations if relying on the old `kebab-case` handling. If you need the old behavior, you might need to pre-process keys or stick to v1.x.","cause":"The conversion logic for `kebab-case` changed in v2.0.0, treating hyphens as word breaks.","error":"Objects with keys like `my-kebab-case` are not converting correctly to `myKebabCase` after upgrading."},{"fix":"Upgrade `ts-case-convert` to version `2.0.5` or newer to ensure `Date` objects are properly preserved.","cause":"Older versions of the library did not preserve `Date` object types during key conversion.","error":"Date objects are being converted to plain objects when using `objectToCamel` or `objectToSnake`."},{"fix":"Upgrade `ts-case-convert` to version `2.0.4` or newer. This version replaced `Buffer` with `Uint8Array` for better cross-environment compatibility.","cause":"Versions prior to 2.0.4 had a hard dependency on the Node.js `Buffer` global, which is not available in browsers.","error":"ReferenceError: Buffer is not defined in browser environment when using `ts-case-convert`."},{"fix":"Upgrade `ts-case-convert` to version `2.0.1` or newer.","cause":"A bug in early 2.x versions (fixed in 2.0.1) caused top-level arrays to be incorrectly typed as objects.","error":"When passing an array as the top-level object to `objectToCamel` or `objectToSnake`, the return type is an object, not an array."}],"ecosystem":"npm"}