0
# Change Case Types
1
2
String case transformation utilities for converting between different naming conventions at the type level.
3
4
## Capabilities
5
6
### CamelCase
7
8
Converts a string type from any common case format (snake_case, kebab-case, PascalCase, etc.) to camelCase.
9
10
```typescript { .api }
11
type CamelCase<Type> = Type extends string ? /* complex conditional logic */ : Type;
12
```
13
14
**Usage Example:**
15
16
```typescript
17
import type { CamelCase } from "ts-essentials";
18
19
// Convert different case formats to camelCase
20
type Example1 = CamelCase<"hello_world">; // "helloWorld"
21
type Example2 = CamelCase<"hello-world">; // "helloWorld"
22
type Example3 = CamelCase<"HelloWorld">; // "helloWorld"
23
type Example4 = CamelCase<"HELLO_WORLD">; // "helloWorld"
24
type Example5 = CamelCase<"hello">; // "hello"
25
26
// Use in object key transformation
27
interface ApiResponse {
28
user_id: number;
29
first_name: string;
30
last_name: string;
31
created_at: string;
32
}
33
34
type CamelCaseKeys<T> = {
35
[K in keyof T as CamelCase<K>]: T[K];
36
};
37
38
type TransformedResponse = CamelCaseKeys<ApiResponse>;
39
// {
40
// userId: number;
41
// firstName: string;
42
// lastName: string;
43
// createdAt: string;
44
// }
45
46
// Function parameter transformation
47
function processData<T extends Record<string, any>>(
48
data: T
49
): { [K in keyof T as CamelCase<K>]: T[K] } {
50
// Implementation would transform keys at runtime
51
return {} as any;
52
}
53
```
54
55
### DeepCamelCaseProperties
56
57
Recursively converts all property keys in a nested object type from any case format to camelCase, preserving the object structure and value types.
58
59
```typescript { .api }
60
type DeepCamelCaseProperties<Type> = Type extends Record<string, unknown>
61
? { [Key in keyof Type as CamelCase<Key>]: DeepCamelCaseProperties<Type[Key]> }
62
: Type;
63
```
64
65
**Usage Example:**
66
67
```typescript
68
import type { DeepCamelCaseProperties } from "ts-essentials";
69
70
// Nested object transformation
71
interface DatabaseUser {
72
user_id: number;
73
first_name: string;
74
user_preferences: {
75
theme_color: string;
76
notification_settings: {
77
email_enabled: boolean;
78
push_enabled: boolean;
79
};
80
};
81
created_at: Date;
82
}
83
84
type CamelCaseUser = DeepCamelCaseProperties<DatabaseUser>;
85
// {
86
// userId: number;
87
// firstName: string;
88
// userPreferences: {
89
// themeColor: string;
90
// notificationSettings: {
91
// emailEnabled: boolean;
92
// pushEnabled: boolean;
93
// };
94
// };
95
// createdAt: Date;
96
// }
97
98
// API response transformation
99
interface ApiUserResponse {
100
user_data: {
101
user_id: number;
102
personal_info: {
103
first_name: string;
104
last_name: string;
105
birth_date: string;
106
};
107
account_settings: {
108
privacy_level: "public" | "private";
109
two_factor_enabled: boolean;
110
};
111
};
112
meta_data: {
113
last_login: string;
114
account_created: string;
115
};
116
}
117
118
type TransformedUser = DeepCamelCaseProperties<ApiUserResponse>;
119
// {
120
// userData: {
121
// userId: number;
122
// personalInfo: {
123
// firstName: string;
124
// lastName: string;
125
// birthDate: string;
126
// };
127
// accountSettings: {
128
// privacyLevel: "public" | "private";
129
// twoFactorEnabled: boolean;
130
// };
131
// };
132
// metaData: {
133
// lastLogin: string;
134
// accountCreated: string;
135
// };
136
// }
137
138
// Array handling - preserves array structure while transforming object keys
139
interface UserWithTags {
140
user_id: number;
141
tag_list: Array<{
142
tag_name: string;
143
tag_color: string;
144
}>;
145
}
146
147
type CamelCaseUserWithTags = DeepCamelCaseProperties<UserWithTags>;
148
// {
149
// userId: number;
150
// tagList: Array<{
151
// tagName: string;
152
// tagColor: string;
153
// }>;
154
// }
155
156
// Generic transformation utility
157
type TransformApiResponse<T> = DeepCamelCaseProperties<T>;
158
159
function transformApiData<T extends Record<string, any>>(
160
data: T
161
): TransformApiResponse<T> {
162
// Implementation would recursively transform keys
163
return {} as TransformApiResponse<T>;
164
}
165
```
166
167
## Advanced Usage
168
169
Combine change case types with other utility types for comprehensive data transformation:
170
171
```typescript
172
import type { DeepCamelCaseProperties, StrictOmit, DeepPartial } from "ts-essentials";
173
174
// Transform case and make partial
175
interface DatabaseConfig {
176
database_host: string;
177
database_port: number;
178
connection_pool_size: number;
179
ssl_enabled: boolean;
180
retry_attempts: number;
181
}
182
183
type PartialCamelConfig = DeepPartial<DeepCamelCaseProperties<DatabaseConfig>>;
184
// {
185
// databaseHost?: string;
186
// databasePort?: number;
187
// connectionPoolSize?: number;
188
// sslEnabled?: boolean;
189
// retryAttempts?: number;
190
// }
191
192
// Transform case and omit sensitive fields
193
interface UserRecord {
194
user_id: number;
195
username: string;
196
password_hash: string;
197
email_address: string;
198
created_at: Date;
199
last_login: Date;
200
}
201
202
type PublicUser = DeepCamelCaseProperties<StrictOmit<UserRecord, "password_hash">>;
203
// {
204
// userId: number;
205
// username: string;
206
// emailAddress: string;
207
// createdAt: Date;
208
// lastLogin: Date;
209
// }
210
211
// Handle complex nested transformations
212
interface ComplexApiResponse {
213
response_metadata: {
214
request_id: string;
215
processing_time_ms: number;
216
};
217
user_data: {
218
basic_info: {
219
user_id: number;
220
display_name: string;
221
};
222
extended_info?: {
223
profile_settings: {
224
theme_preference: string;
225
language_code: string;
226
};
227
};
228
}[];
229
}
230
231
type TransformedComplexResponse = DeepCamelCaseProperties<ComplexApiResponse>;
232
// {
233
// responseMetadata: {
234
// requestId: string;
235
// processingTimeMs: number;
236
// };
237
// userData: {
238
// basicInfo: {
239
// userId: number;
240
// displayName: string;
241
// };
242
// extendedInfo?: {
243
// profileSettings: {
244
// themePreference: string;
245
// languageCode: string;
246
// };
247
// };
248
// }[];
249
// }
250
```