CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-essentials

All essential TypeScript types in one place providing 70+ utility types and helper functions.

Pending
Overview
Eval results
Files

change-case-types.mddocs/

Change Case Types

String case transformation utilities for converting between different naming conventions at the type level.

Capabilities

CamelCase

Converts a string type from any common case format (snake_case, kebab-case, PascalCase, etc.) to camelCase.

type CamelCase<Type> = Type extends string ? /* complex conditional logic */ : Type;

Usage Example:

import type { CamelCase } from "ts-essentials";

// Convert different case formats to camelCase
type Example1 = CamelCase<"hello_world">; // "helloWorld"
type Example2 = CamelCase<"hello-world">; // "helloWorld" 
type Example3 = CamelCase<"HelloWorld">; // "helloWorld"
type Example4 = CamelCase<"HELLO_WORLD">; // "helloWorld"
type Example5 = CamelCase<"hello">; // "hello"

// Use in object key transformation
interface ApiResponse {
  user_id: number;
  first_name: string;
  last_name: string;
  created_at: string;
}

type CamelCaseKeys<T> = {
  [K in keyof T as CamelCase<K>]: T[K];
};

type TransformedResponse = CamelCaseKeys<ApiResponse>;
// {
//   userId: number;
//   firstName: string;
//   lastName: string;
//   createdAt: string;
// }

// Function parameter transformation
function processData<T extends Record<string, any>>(
  data: T
): { [K in keyof T as CamelCase<K>]: T[K] } {
  // Implementation would transform keys at runtime
  return {} as any;
}

DeepCamelCaseProperties

Recursively converts all property keys in a nested object type from any case format to camelCase, preserving the object structure and value types.

type DeepCamelCaseProperties<Type> = Type extends Record<string, unknown>
  ? { [Key in keyof Type as CamelCase<Key>]: DeepCamelCaseProperties<Type[Key]> }
  : Type;

Usage Example:

import type { DeepCamelCaseProperties } from "ts-essentials";

// Nested object transformation
interface DatabaseUser {
  user_id: number;
  first_name: string;
  user_preferences: {
    theme_color: string;
    notification_settings: {
      email_enabled: boolean;
      push_enabled: boolean;
    };
  };
  created_at: Date;
}

type CamelCaseUser = DeepCamelCaseProperties<DatabaseUser>;
// {
//   userId: number;
//   firstName: string;
//   userPreferences: {
//     themeColor: string;
//     notificationSettings: {
//       emailEnabled: boolean;
//       pushEnabled: boolean;
//     };
//   };
//   createdAt: Date;
// }

// API response transformation
interface ApiUserResponse {
  user_data: {
    user_id: number;
    personal_info: {
      first_name: string;
      last_name: string;
      birth_date: string;
    };
    account_settings: {
      privacy_level: "public" | "private";
      two_factor_enabled: boolean;
    };
  };
  meta_data: {
    last_login: string;
    account_created: string;
  };
}

type TransformedUser = DeepCamelCaseProperties<ApiUserResponse>;
// {
//   userData: {
//     userId: number;
//     personalInfo: {
//       firstName: string;
//       lastName: string;
//       birthDate: string;
//     };
//     accountSettings: {
//       privacyLevel: "public" | "private";
//       twoFactorEnabled: boolean;
//     };
//   };
//   metaData: {
//     lastLogin: string;
//     accountCreated: string;
//   };
// }

// Array handling - preserves array structure while transforming object keys
interface UserWithTags {
  user_id: number;
  tag_list: Array<{
    tag_name: string;
    tag_color: string;
  }>;
}

type CamelCaseUserWithTags = DeepCamelCaseProperties<UserWithTags>;
// {
//   userId: number;
//   tagList: Array<{
//     tagName: string;
//     tagColor: string;
//   }>;
// }

// Generic transformation utility
type TransformApiResponse<T> = DeepCamelCaseProperties<T>;

function transformApiData<T extends Record<string, any>>(
  data: T
): TransformApiResponse<T> {
  // Implementation would recursively transform keys
  return {} as TransformApiResponse<T>;
}

Advanced Usage

Combine change case types with other utility types for comprehensive data transformation:

import type { DeepCamelCaseProperties, StrictOmit, DeepPartial } from "ts-essentials";

// Transform case and make partial
interface DatabaseConfig {
  database_host: string;
  database_port: number;
  connection_pool_size: number;
  ssl_enabled: boolean;
  retry_attempts: number;
}

type PartialCamelConfig = DeepPartial<DeepCamelCaseProperties<DatabaseConfig>>;
// {
//   databaseHost?: string;
//   databasePort?: number;
//   connectionPoolSize?: number;
//   sslEnabled?: boolean;
//   retryAttempts?: number;
// }

// Transform case and omit sensitive fields
interface UserRecord {
  user_id: number;
  username: string;
  password_hash: string;
  email_address: string;
  created_at: Date;
  last_login: Date;
}

type PublicUser = DeepCamelCaseProperties<StrictOmit<UserRecord, "password_hash">>;
// {
//   userId: number;
//   username: string;
//   emailAddress: string;
//   createdAt: Date;
//   lastLogin: Date;
// }

// Handle complex nested transformations
interface ComplexApiResponse {
  response_metadata: {
    request_id: string;
    processing_time_ms: number;
  };
  user_data: {
    basic_info: {
      user_id: number;
      display_name: string;
    };
    extended_info?: {
      profile_settings: {
        theme_preference: string;
        language_code: string;
      };
    };
  }[];
}

type TransformedComplexResponse = DeepCamelCaseProperties<ComplexApiResponse>;
// {
//   responseMetadata: {
//     requestId: string;
//     processingTimeMs: number;
//   };
//   userData: {
//     basicInfo: {
//       userId: number;
//       displayName: string;
//     };
//     extendedInfo?: {
//       profileSettings: {
//         themePreference: string;
//         languageCode: string;
//       };
//     };
//   }[];
// }

Install with Tessl CLI

npx tessl i tessl/npm-ts-essentials

docs

array-tuple-types.md

basic-types.md

change-case-types.md

deep-wrapper-types.md

function-types.md

index.md

key-types.md

mark-wrapper-types.md

type-checkers.md

utility-functions.md

utility-types.md

tile.json