-
-
Notifications
You must be signed in to change notification settings - Fork 547
/
delimiter-cased-properties-deep.d.ts
84 lines (75 loc) · 2.4 KB
/
delimiter-cased-properties-deep.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import type {DelimiterCase} from './delimiter-case';
import type {NonRecursiveType} from './internal';
import type {UnknownArray} from './unknown-array';
/**
Convert object properties to delimiter case recursively.
This can be useful when, for example, converting some API types from a different style.
@see DelimiterCase
@see DelimiterCasedProperties
@example
```
import type {DelimiterCasedPropertiesDeep} from 'type-fest';
interface User {
userId: number;
userName: string;
}
interface UserWithFriends {
userInfo: User;
userFriends: User[];
}
const result: DelimiterCasedPropertiesDeep<UserWithFriends, '-'> = {
'user-info': {
'user-id': 1,
'user-name': 'Tom',
},
'user-friends': [
{
'user-id': 2,
'user-name': 'Jerry',
},
{
'user-id': 3,
'user-name': 'Spike',
},
],
};
```
@category Change case
@category Template literal
@category Object
*/
export type DelimiterCasedPropertiesDeep<
Value,
Delimiter extends string,
> = Value extends NonRecursiveType
? Value
: Value extends UnknownArray
? DelimiterCasedPropertiesArrayDeep<Value, Delimiter>
: Value extends Set<infer U>
? Set<DelimiterCasedPropertiesDeep<U, Delimiter>> : {
[K in keyof Value as DelimiterCase<
K,
Delimiter
>]: DelimiterCasedPropertiesDeep<Value[K], Delimiter>;
};
// This is a copy of CamelCasedPropertiesArrayDeep (see: camel-cased-properties-deep.d.ts).
// These types should be kept in sync.
type DelimiterCasedPropertiesArrayDeep<Value extends UnknownArray, Delimiter extends string> =
Value extends []
? []
// Tailing spread array
: Value extends [infer U, ...infer V]
? [DelimiterCasedPropertiesDeep<U, Delimiter>, ...DelimiterCasedPropertiesDeep<V, Delimiter>]
: Value extends readonly [infer U, ...infer V]
? readonly [DelimiterCasedPropertiesDeep<U, Delimiter>, ...DelimiterCasedPropertiesDeep<V, Delimiter>]
// Leading spread array
: Value extends readonly [...infer U, infer V]
? [...DelimiterCasedPropertiesDeep<U, Delimiter>, DelimiterCasedPropertiesDeep<V, Delimiter>]
: Value extends readonly [...infer U, infer V]
? readonly [...DelimiterCasedPropertiesDeep<U, Delimiter>, DelimiterCasedPropertiesDeep<V, Delimiter>]
// Array
: Value extends Array<infer U>
? Array<DelimiterCasedPropertiesDeep<U, Delimiter>>
: Value extends ReadonlyArray<infer U>
? ReadonlyArray<DelimiterCasedPropertiesDeep<U, Delimiter>>
: never;