@qlover/fe-corekit 3.1.1 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,193 @@
1
+ /**
2
+ * Extract all possible value types from an object type
3
+ *
4
+ * This utility type creates a union of all property value types from an object,
5
+ * which is useful for type narrowing, validation, and working with object values
6
+ * in a type-safe manner.
7
+ *
8
+ * Use cases:
9
+ * - Type narrowing for object values
10
+ * - Creating union types from object properties
11
+ * - Validating values against allowed object values
12
+ * - Generic constraints for value types
13
+ *
14
+ * @since `1.0.14`
15
+ *
16
+ * @example Basic usage
17
+ * ```ts
18
+ * type User = { id: number; name: string; active: boolean };
19
+ * type UserValue = ValueOf<User>; // number | string | boolean
20
+ *
21
+ * function processValue(value: UserValue) {
22
+ * // value can be number, string, or boolean
23
+ * }
24
+ * ```
25
+ *
26
+ * @example With nested objects
27
+ * ```ts
28
+ * type Config = {
29
+ * database: { host: string; port: number };
30
+ * cache: { enabled: boolean; ttl: number };
31
+ * };
32
+ * type ConfigValue = ValueOf<Config>;
33
+ * // { host: string; port: number } | { enabled: boolean; ttl: number }
34
+ * ```
35
+ *
36
+ * @example Type validation
37
+ * ```ts
38
+ * type Status = { pending: 'pending'; active: 'active'; done: 'done' };
39
+ * type StatusValue = ValueOf<Status>; // 'pending' | 'active' | 'done'
40
+ *
41
+ * function isValidStatus(value: string): value is StatusValue {
42
+ * return ['pending', 'active', 'done'].includes(value);
43
+ * }
44
+ * ```
45
+ */
46
+ type ValueOf<T> = T[keyof T];
47
+ /**
48
+ * Create an intersection type containing only common properties
49
+ *
50
+ * This utility type extracts properties that exist in both types and creates
51
+ * a new type with those properties. The resulting type contains a union of
52
+ * the property types from both input types.
53
+ *
54
+ * Use cases:
55
+ * - Finding common properties between types
56
+ * - Type merging with overlap handling
57
+ * - API response type composition
58
+ * - Shared interface extraction
59
+ *
60
+ * @since `1.0.14`
61
+ *
62
+ * @example Basic usage
63
+ * ```ts
64
+ * type User = { id: number; name: string; email: string };
65
+ * type Admin = { id: number; name: string; role: string };
66
+ * type Common = Intersection<User, Admin>;
67
+ * // { id: number | number; name: string | string }
68
+ * // Simplifies to: { id: number; name: string }
69
+ * ```
70
+ *
71
+ * @example API response merging
72
+ * ```ts
73
+ * type APIResponse = { status: number; data: unknown; timestamp: string };
74
+ * type CachedResponse = { status: number; data: unknown; cached: boolean };
75
+ * type SharedFields = Intersection<APIResponse, CachedResponse>;
76
+ * // { status: number; data: unknown }
77
+ * ```
78
+ *
79
+ * @example Type compatibility checking
80
+ * ```ts
81
+ * type FormData = { username: string; email: string; password: string };
82
+ * type UserProfile = { username: string; email: string; bio: string };
83
+ * type EditableFields = Intersection<FormData, UserProfile>;
84
+ * // { username: string; email: string }
85
+ * ```
86
+ */
87
+ type Intersection<T1, T2> = {
88
+ [P in keyof T1 & keyof T2]: T1[P] | T2[P];
89
+ };
90
+ /**
91
+ * Create a deeply partial type with optional nested properties
92
+ *
93
+ * This utility type recursively makes all properties optional, including
94
+ * nested object properties. Unlike TypeScript's built-in `Partial<T>`,
95
+ * this type applies the optional modifier to all levels of nesting.
96
+ *
97
+ * Use cases:
98
+ * - Partial updates for nested objects
99
+ * - Optional configuration objects
100
+ * - Flexible API request payloads
101
+ * - Form state management with partial data
102
+ *
103
+ * @since `3.0.0`
104
+ *
105
+ * @example Basic usage
106
+ * ```ts
107
+ * type Config = {
108
+ * server: {
109
+ * host: string;
110
+ * port: number;
111
+ * ssl: {
112
+ * enabled: boolean;
113
+ * cert: string;
114
+ * };
115
+ * };
116
+ * database: {
117
+ * url: string;
118
+ * };
119
+ * };
120
+ *
121
+ * type PartialConfig = PartialDeep<Config>;
122
+ * // All properties are optional, including nested ones:
123
+ * // {
124
+ * // server?: {
125
+ * // host?: string;
126
+ * // port?: number;
127
+ * // ssl?: {
128
+ * // enabled?: boolean;
129
+ * // cert?: string;
130
+ * // };
131
+ * // };
132
+ * // database?: {
133
+ * // url?: string;
134
+ * // };
135
+ * // }
136
+ * ```
137
+ *
138
+ * @example Partial updates
139
+ * ```ts
140
+ * interface User {
141
+ * profile: {
142
+ * name: string;
143
+ * email: string;
144
+ * settings: {
145
+ * theme: string;
146
+ * notifications: boolean;
147
+ * };
148
+ * };
149
+ * }
150
+ *
151
+ * function updateUser(userId: string, updates: PartialDeep<User>) {
152
+ * // Can update any nested property without providing all fields
153
+ * }
154
+ *
155
+ * updateUser('123', {
156
+ * profile: {
157
+ * settings: {
158
+ * theme: 'dark' // Only update theme, other fields remain unchanged
159
+ * }
160
+ * }
161
+ * });
162
+ * ```
163
+ *
164
+ * @example Form state management
165
+ * ```ts
166
+ * interface FormState {
167
+ * user: {
168
+ * name: string;
169
+ * contact: {
170
+ * email: string;
171
+ * phone: string;
172
+ * };
173
+ * };
174
+ * }
175
+ *
176
+ * type PartialFormState = PartialDeep<FormState>;
177
+ *
178
+ * const formState: PartialFormState = {
179
+ * user: {
180
+ * contact: {
181
+ * email: 'user@example.com'
182
+ * // phone is optional
183
+ * }
184
+ * // name is optional
185
+ * }
186
+ * };
187
+ * ```
188
+ */
189
+ type PartialDeep<T> = {
190
+ [K in keyof T]?: T[K] extends object ? PartialDeep<T[K]> : T[K];
191
+ };
192
+
193
+ export type { Intersection, PartialDeep, ValueOf };
package/dist/common.js ADDED
File without changes