forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamel-cased-properties-deep.ts
More file actions
92 lines (73 loc) · 2.64 KB
/
camel-cased-properties-deep.ts
File metadata and controls
92 lines (73 loc) · 2.64 KB
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
85
86
87
88
89
90
91
92
import {expectType} from 'tsd';
import type {CamelCasedPropertiesDeep, Tagged} from '../index.d.ts';
declare const foo: CamelCasedPropertiesDeep<{A: {B: number; C: Array<{D: string}>}}>;
expectType<{a: {b: number; c: Array<{d: string}>}}>(foo);
declare const fooBar: CamelCasedPropertiesDeep<() => {a: string}>;
expectType<() => {a: string}>(fooBar);
declare const bar: CamelCasedPropertiesDeep<Set<{fooBar: string}>>;
expectType<Set<{fooBar: string}>>(bar);
type bazBizDeep = {fooBAR: number; baz: {fooBAR: Array<{BARFoo: string}>}};
declare const baz: CamelCasedPropertiesDeep<bazBizDeep, {preserveConsecutiveUppercase: true}>;
expectType<{fooBAR: number; baz: {fooBAR: Array<{bARFoo: string}>}}>(baz);
declare const biz: CamelCasedPropertiesDeep<bazBizDeep>;
expectType<{fooBar: number; baz: {fooBar: Array<{barFoo: string}>}}>(biz);
declare const tuple: CamelCasedPropertiesDeep<{tuple: [number, string, {D: string}]}>;
expectType<{tuple: [number, string, {d: string}]}>(tuple);
// Verify example
type UserRole = Tagged<string, 'Role'>;
type User = {
UserId: number;
UserName: string;
Date: Date;
RegExp: RegExp;
Role: UserRole;
};
type UserPunctuated = {
'user::id': number;
'user::name': string;
date: Date;
'reg::exp': RegExp;
Role: UserRole;
};
type UserWithFriends = {
UserInfo: User;
UserFriends: User[];
};
type UserWithFriendsPunctuated = {
'user@info': UserPunctuated;
'user#friends': UserPunctuated[];
};
const role = 'someRole' as UserRole;
const result: CamelCasedPropertiesDeep<UserWithFriends> = {
userInfo: {
userId: 1,
userName: 'Tom',
date: new Date(),
regExp: /.*/,
role,
},
userFriends: [
{
userId: 2,
userName: 'Jerry',
date: new Date(),
regExp: /.*/,
role,
},
{
userId: 3,
userName: 'Spike',
date: new Date(),
regExp: /.*/,
role,
},
],
};
expectType<CamelCasedPropertiesDeep<UserWithFriends>>(result);
expectType<CamelCasedPropertiesDeep<UserWithFriendsPunctuated, {splitOnPunctuation: true}>>(result);
expectType<{fooBar: unknown}>({} as CamelCasedPropertiesDeep<{foo_bar: unknown}>);
expectType<{fooBar: {barBaz: unknown}; biz: unknown}>({} as CamelCasedPropertiesDeep<{foo_bar: {bar_baz: unknown}; biz: unknown}>);
expectType<{fooBar: any}>({} as CamelCasedPropertiesDeep<{foo_bar: any}>);
expectType<{fooBar: {barBaz: any}; biz: any}>({} as CamelCasedPropertiesDeep<{foo_bar: {bar_baz: any}; biz: any}>);
expectType<{'fooBar': unknown}>({} as CamelCasedPropertiesDeep<{'foo::bar': unknown}, {splitOnPunctuation: true}>);
expectType<{'fooBar': {'barBaz': unknown}; biz: unknown}>({} as CamelCasedPropertiesDeep<{'foo::bar': {'bar@baz': unknown}; biz: unknown}, {splitOnPunctuation: true}>);