UNPKG

1.22 kBTypeScriptView Raw
1import type {CamelCaseOptions} from './camel-case';
2import type {PascalCase} from './pascal-case';
3
4/**
5Convert object properties to pascal case recursively.
6
7This can be useful when, for example, converting some API types from a different style.
8
9@see PascalCase
10@see PascalCasedProperties
11
12@example
13```
14import type {PascalCasedPropertiesDeep} from 'type-fest';
15
16interface User {
17 userId: number;
18 userName: string;
19}
20
21interface UserWithFriends {
22 userInfo: User;
23 userFriends: User[];
24}
25
26const result: PascalCasedPropertiesDeep<UserWithFriends> = {
27 UserInfo: {
28 UserId: 1,
29 UserName: 'Tom',
30 },
31 UserFriends: [
32 {
33 UserId: 2,
34 UserName: 'Jerry',
35 },
36 {
37 UserId: 3,
38 UserName: 'Spike',
39 },
40 ],
41};
42```
43
44@category Change case
45@category Template literal
46@category Object
47*/
48export type PascalCasedPropertiesDeep<Value, Options extends CamelCaseOptions = {preserveConsecutiveUppercase: true}> = Value extends Function | Date | RegExp
49 ? Value
50 : Value extends Array<infer U>
51 ? Array<PascalCasedPropertiesDeep<U, Options>>
52 : Value extends Set<infer U>
53 ? Set<PascalCasedPropertiesDeep<U, Options>> : {
54 [K in keyof Value as PascalCase<K, Options>]: PascalCasedPropertiesDeep<Value[K], Options>;
55 };