@kakasoo/deep-strict-types 2.0.4 → 2.0.6
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.
- package/README.md +141 -267
- package/bin/src/functions/DeepStrictAssert.d.ts +19 -0
- package/bin/src/functions/DeepStrictAssert.d.ts.map +1 -1
- package/bin/src/functions/DeepStrictAssert.js +19 -0
- package/bin/src/functions/DeepStrictAssert.js.map +1 -1
- package/bin/src/functions/DeepStrictObjectKeys.d.ts +27 -0
- package/bin/src/functions/DeepStrictObjectKeys.d.ts.map +1 -1
- package/bin/src/functions/DeepStrictObjectKeys.js +20 -0
- package/bin/src/functions/DeepStrictObjectKeys.js.map +1 -1
- package/bin/src/types/DeepDateToString.d.ts +22 -4
- package/bin/src/types/DeepDateToString.d.ts.map +1 -1
- package/bin/src/types/DeepStrictMerge.d.ts +11 -0
- package/bin/src/types/DeepStrictMerge.d.ts.map +1 -1
- package/bin/src/types/DeepStrictObjectKeys.d.ts +4 -3
- package/bin/src/types/DeepStrictObjectKeys.d.ts.map +1 -1
- package/bin/src/types/DeepStrictObjectLastKeys.d.ts +18 -3
- package/bin/src/types/DeepStrictObjectLastKeys.d.ts.map +1 -1
- package/bin/src/types/DeepStrictOmit.d.ts +13 -0
- package/bin/src/types/DeepStrictOmit.d.ts.map +1 -1
- package/bin/src/types/DeepStrictUnbrand.d.ts +13 -0
- package/bin/src/types/DeepStrictUnbrand.d.ts.map +1 -1
- package/bin/src/types/ElementOf.d.ts +28 -1
- package/bin/src/types/ElementOf.d.ts.map +1 -1
- package/bin/src/types/Equal.d.ts +31 -2
- package/bin/src/types/Equal.d.ts.map +1 -1
- package/bin/src/types/GetType.d.ts +35 -13
- package/bin/src/types/GetType.d.ts.map +1 -1
- package/bin/src/types/IsAny.d.ts +11 -6
- package/bin/src/types/IsAny.d.ts.map +1 -1
- package/bin/src/types/IsUnion.d.ts +26 -6
- package/bin/src/types/IsUnion.d.ts.map +1 -1
- package/bin/src/types/RemoveAfterDot.d.ts +22 -0
- package/bin/src/types/RemoveAfterDot.d.ts.map +1 -1
- package/bin/src/types/RemoveArraySymbol.d.ts +16 -4
- package/bin/src/types/RemoveArraySymbol.d.ts.map +1 -1
- package/bin/src/types/RemoveLastProperty.d.ts +21 -0
- package/bin/src/types/RemoveLastProperty.d.ts.map +1 -1
- package/bin/src/types/StringToDeepObject.d.ts +25 -0
- package/bin/src/types/StringToDeepObject.d.ts.map +1 -1
- package/bin/src/types/ValueType.d.ts +17 -0
- package/bin/src/types/ValueType.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,362 +1,236 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kakasoo/deep-strict-types
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@kakasoo/deep-strict-types)
|
|
4
|
+
[](https://opensource.org/licenses/ISC)
|
|
5
|
+
[](https://www.typescriptlang.org/)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
Type-safe `Pick`, `Omit`, and key extraction for deeply nested TypeScript objects and arrays.
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
2. [DeepStrictObjectKeys](#deepstrictobjectkeys)
|
|
9
|
-
3. [DeepStrictOmit](#deepstrictomit)
|
|
10
|
-
4. [DeepStrictPick](#deepstrictpick)
|
|
11
|
-
5. [StringToDeepObject](#stringtodeepobject)
|
|
12
|
-
6. [DeepStrictMerge](#deepstrictmerge)
|
|
13
|
-
7. [DeepDateToString](#deepdatetostring)
|
|
14
|
-
|
|
15
|
-
## Introduction
|
|
16
|
-
|
|
17
|
-
DeepStrictTypes is a tool that takes TypeScript’s type manipulation to the next level.
|
|
18
|
-
It helps you safely perform tasks like `Omit` and `Pick` even with complex nested objects or arrays.
|
|
19
|
-
By addressing the limitations of TypeScript’s built-in utility types, it allows you to easily handle internal keys with strict and precise type inference.
|
|
20
|
-
|
|
21
|
-
Key features include:
|
|
22
|
-
|
|
23
|
-
- **Safe Nested Key Extraction:** It extracts all keys from within an object, boosting type safety.
|
|
24
|
-
- **Precise Type Manipulation:** You can pick or omit only the keys you need even in deeply nested structures, making it easier to work with complex data.
|
|
25
|
-
- **Unbranding and Merging:** It removes unnecessary constraints from branded types and safely merges multiple types.
|
|
26
|
-
- **Utility Function Support (Experimental):** It even provides runtime functions to further ensure type safety during development.
|
|
27
|
-
|
|
28
|
-
Below is a GIF showing an example of how to use the library.
|
|
9
|
+
[한국어 설명](./docs/README_KO.md)
|
|
29
10
|
|
|
30
11
|

|
|
31
12
|
|
|
32
|
-
##
|
|
33
|
-
|
|
34
|
-
`DeepStrictObjectKeys` extracts all keys from a nested object, preserving its hierarchical structure as a union of string paths.
|
|
35
|
-
That means you can access not only top-level keys but also nested keys using dot notation or, for arrays, using `[*]`.
|
|
36
|
-
|
|
37
|
-
### Key Features
|
|
13
|
+
## Installation
|
|
38
14
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
### Example
|
|
15
|
+
```bash
|
|
16
|
+
npm install @kakasoo/deep-strict-types
|
|
17
|
+
```
|
|
44
18
|
|
|
45
|
-
|
|
19
|
+
## Quick Start
|
|
46
20
|
|
|
47
21
|
```typescript
|
|
48
|
-
|
|
49
|
-
|
|
22
|
+
import { DeepStrictObjectKeys, DeepStrictPick, DeepStrictOmit } from '@kakasoo/deep-strict-types';
|
|
23
|
+
|
|
24
|
+
type User = {
|
|
25
|
+
id: string;
|
|
26
|
+
profile: {
|
|
50
27
|
name: string;
|
|
51
|
-
|
|
52
|
-
city: string;
|
|
53
|
-
zip: number;
|
|
54
|
-
};
|
|
28
|
+
age: number;
|
|
55
29
|
};
|
|
30
|
+
posts: {
|
|
31
|
+
title: string;
|
|
32
|
+
tags: string[];
|
|
33
|
+
}[];
|
|
56
34
|
};
|
|
57
35
|
|
|
58
|
-
//
|
|
59
|
-
type Keys = DeepStrictObjectKeys<
|
|
60
|
-
|
|
36
|
+
// Extract all nested key paths
|
|
37
|
+
type Keys = DeepStrictObjectKeys<User>;
|
|
38
|
+
// "id" | "profile" | "profile.name" | "profile.age" | "posts" | "posts[*].title" | "posts[*].tags"
|
|
61
39
|
|
|
62
|
-
|
|
40
|
+
// Pick only what you need
|
|
41
|
+
type NameOnly = DeepStrictPick<User, 'profile.name'>;
|
|
42
|
+
// { profile: { name: string } }
|
|
63
43
|
|
|
64
|
-
|
|
65
|
-
type
|
|
66
|
-
|
|
44
|
+
// Remove what you don't need
|
|
45
|
+
type NoAge = DeepStrictOmit<User, 'profile.age'>;
|
|
46
|
+
// { id: string; profile: { name: string }; posts: { title: string; tags: string[] }[] }
|
|
67
47
|
```
|
|
68
48
|
|
|
69
|
-
##
|
|
70
|
-
|
|
71
|
-
`DeepStrictOmit` creates a new type by removing specified keys from a nested object type.
|
|
72
|
-
Similar to the built-in `Omit`, it lets you precisely specify key paths—even in nested structures and arrays—to remove unwanted properties.
|
|
73
|
-
|
|
74
|
-
### Key Features
|
|
75
|
-
|
|
76
|
-
- **Omit Nested Keys:** You can specify a nested key path like `"user.profile.name"` to remove just that property.
|
|
77
|
-
- **Handles Arrays:** It applies the same logic to objects within arrays, so you can remove a key from every element.
|
|
78
|
-
- **Accurate Type Inference:** It preserves the rest of the object’s structure and types after omission.
|
|
79
|
-
- **Supports Branded Types:** It works safely with branded types, removing unnecessary constraints.
|
|
49
|
+
## Core Types
|
|
80
50
|
|
|
81
|
-
###
|
|
51
|
+
### `DeepStrictObjectKeys<T>`
|
|
82
52
|
|
|
83
|
-
|
|
53
|
+
Extracts all keys from a nested object as a union of dot-notation string paths. Arrays use `[*]` notation.
|
|
84
54
|
|
|
85
55
|
```typescript
|
|
86
|
-
// Define an example object type
|
|
87
56
|
type Example = {
|
|
88
57
|
user: {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
name: string;
|
|
92
|
-
age: number;
|
|
93
|
-
email: string;
|
|
94
|
-
};
|
|
95
|
-
posts: {
|
|
96
|
-
title: string;
|
|
97
|
-
content: string;
|
|
98
|
-
meta: {
|
|
99
|
-
likes: number;
|
|
100
|
-
shares: number;
|
|
101
|
-
};
|
|
102
|
-
}[];
|
|
58
|
+
name: string;
|
|
59
|
+
address: { city: string; zip: number };
|
|
103
60
|
};
|
|
104
61
|
};
|
|
105
62
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
/*
|
|
110
|
-
Resulting type Omitted:
|
|
111
|
-
{
|
|
112
|
-
user: {
|
|
113
|
-
id: string;
|
|
114
|
-
profile: {
|
|
115
|
-
name: string;
|
|
116
|
-
age: number;
|
|
117
|
-
};
|
|
118
|
-
posts: {
|
|
119
|
-
title: string;
|
|
120
|
-
content: string;
|
|
121
|
-
meta: {
|
|
122
|
-
likes: number;
|
|
123
|
-
};
|
|
124
|
-
}[];
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
*/
|
|
63
|
+
type Keys = DeepStrictObjectKeys<Example>;
|
|
64
|
+
// "user" | "user.name" | "user.address" | "user.address.city" | "user.address.zip"
|
|
128
65
|
```
|
|
129
66
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
## DeepStrictPick
|
|
133
|
-
|
|
134
|
-
`DeepStrictPick` creates a new type by selecting only the specified keys from a nested object type.
|
|
135
|
-
It works like the built-in `Pick` but lets you precisely choose key paths—even in nested structures and arrays—so you only get the properties you need.
|
|
136
|
-
|
|
137
|
-
### Key Features
|
|
67
|
+
```typescript
|
|
68
|
+
type WithArray = { items: { name: string; price: number }[] };
|
|
138
69
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
- **Flexible:** You can specify multiple nested keys at once.
|
|
70
|
+
type Keys = DeepStrictObjectKeys<WithArray>;
|
|
71
|
+
// "items" | "items[*].name" | "items[*].price"
|
|
72
|
+
```
|
|
143
73
|
|
|
144
|
-
###
|
|
74
|
+
### `DeepStrictPick<T, K>`
|
|
145
75
|
|
|
146
|
-
|
|
76
|
+
Creates a new type by selecting only the specified nested keys, preserving the object structure.
|
|
147
77
|
|
|
148
78
|
```typescript
|
|
149
|
-
// Define an example object type
|
|
150
79
|
type Example = {
|
|
151
80
|
user: {
|
|
152
81
|
id: string;
|
|
153
|
-
profile: {
|
|
154
|
-
|
|
155
|
-
age: number;
|
|
156
|
-
email: string;
|
|
157
|
-
};
|
|
158
|
-
posts: {
|
|
159
|
-
title: string;
|
|
160
|
-
content: string;
|
|
161
|
-
meta: {
|
|
162
|
-
likes: number;
|
|
163
|
-
shares: number;
|
|
164
|
-
};
|
|
165
|
-
}[];
|
|
82
|
+
profile: { name: string; age: number; email: string };
|
|
83
|
+
posts: { title: string; content: string; meta: { likes: number; shares: number } }[];
|
|
166
84
|
};
|
|
167
85
|
};
|
|
168
86
|
|
|
169
|
-
// Pick only the keys 'user.profile.name' and 'user.posts[*].meta.likes'
|
|
170
87
|
type Picked = DeepStrictPick<Example, 'user.profile.name' | 'user.posts[*].meta.likes'>;
|
|
171
|
-
|
|
172
88
|
/*
|
|
173
|
-
Resulting type Picked:
|
|
174
89
|
{
|
|
175
90
|
user: {
|
|
176
|
-
profile: {
|
|
177
|
-
|
|
178
|
-
};
|
|
179
|
-
posts: {
|
|
180
|
-
meta: {
|
|
181
|
-
likes: number;
|
|
182
|
-
};
|
|
183
|
-
}[];
|
|
91
|
+
profile: { name: string };
|
|
92
|
+
posts: { meta: { likes: number } }[];
|
|
184
93
|
};
|
|
185
94
|
}
|
|
186
95
|
*/
|
|
187
96
|
```
|
|
188
97
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
## StringToDeepObject
|
|
192
|
-
|
|
193
|
-
`StringToDeepObject` takes a string path in dot notation and generates a nested object type corresponding to that path.
|
|
194
|
-
It parses the path string step by step, building a nested object and assigning the desired type to the final property.
|
|
98
|
+
### `DeepStrictOmit<T, K>`
|
|
195
99
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
- **Parses Path Strings:** Converts a string like "user.profile.name" into an object where each segment becomes a key.
|
|
199
|
-
- **Dynamically Creates Objects:** Automatically builds a nested object based on the path, assigning the specified type at the end.
|
|
200
|
-
- **Merges Union Types:** If you pass a union of path strings, it merges the resulting objects into one combined type.
|
|
201
|
-
- **Type Safe:** Handles string paths safely within the type system to accurately represent nested structures.
|
|
202
|
-
|
|
203
|
-
### Example
|
|
100
|
+
Creates a new type by removing the specified nested keys.
|
|
204
101
|
|
|
205
102
|
```typescript
|
|
206
|
-
|
|
207
|
-
type DeepObj = StringToDeepObject<'user.profile.name', string>;
|
|
208
|
-
|
|
103
|
+
type Omitted = DeepStrictOmit<Example, 'user.profile.email' | 'user.posts[*].meta.shares'>;
|
|
209
104
|
/*
|
|
210
|
-
Resulting type DeepObj:
|
|
211
105
|
{
|
|
212
106
|
user: {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
};
|
|
107
|
+
id: string;
|
|
108
|
+
profile: { name: string; age: number };
|
|
109
|
+
posts: { title: string; content: string; meta: { likes: number } }[];
|
|
216
110
|
};
|
|
217
111
|
}
|
|
218
112
|
*/
|
|
113
|
+
```
|
|
219
114
|
|
|
220
|
-
|
|
221
|
-
type DeepNumberObj = StringToDeepObject<'settings.display.brightness', number>;
|
|
115
|
+
### `DeepStrictMerge<Target, Source>`
|
|
222
116
|
|
|
223
|
-
|
|
224
|
-
Resulting type DeepNumberObj:
|
|
225
|
-
{
|
|
226
|
-
settings: {
|
|
227
|
-
display: {
|
|
228
|
-
brightness: number;
|
|
229
|
-
};
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
*/
|
|
117
|
+
Deeply merges two object types. When both types share a key, `Target` takes precedence.
|
|
233
118
|
|
|
234
|
-
|
|
235
|
-
type
|
|
119
|
+
```typescript
|
|
120
|
+
type A = { user: { id: string; profile: { name: string } } };
|
|
121
|
+
type B = { user: { profile: { email: string }; settings: { theme: string } } };
|
|
236
122
|
|
|
123
|
+
type Merged = DeepStrictMerge<A, B>;
|
|
237
124
|
/*
|
|
238
|
-
Resulting type MergedObj:
|
|
239
125
|
{
|
|
240
126
|
user: {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
};
|
|
127
|
+
id: string;
|
|
128
|
+
profile: { name: string; email: string };
|
|
129
|
+
settings: { theme: string };
|
|
245
130
|
};
|
|
246
131
|
}
|
|
247
132
|
*/
|
|
248
133
|
```
|
|
249
134
|
|
|
250
|
-
|
|
135
|
+
Arrays of objects are also merged element-wise:
|
|
251
136
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
### Key Features
|
|
137
|
+
```typescript
|
|
138
|
+
type Merged = DeepStrictMerge<{ a: number }[], { b: string }[]>;
|
|
139
|
+
// { a: number; b: string }[]
|
|
140
|
+
```
|
|
258
141
|
|
|
259
|
-
|
|
260
|
-
- **Accurate Type Inference:** Each object’s type information is retained in the merged result, ensuring type safety.
|
|
261
|
-
- **Conflict Resolution:** When the same key exists in multiple objects, it resolves the conflict according to defined rules.
|
|
262
|
-
- **Flexible:** You can merge several object types at once, making it easy to manage complex data structures.
|
|
142
|
+
### `GetType<T, K>`
|
|
263
143
|
|
|
264
|
-
|
|
144
|
+
Extracts the type at a specific nested path.
|
|
265
145
|
|
|
266
146
|
```typescript
|
|
267
|
-
|
|
268
|
-
type ObjA = {
|
|
147
|
+
type Data = {
|
|
269
148
|
user: {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
name: string;
|
|
273
|
-
age: number;
|
|
274
|
-
};
|
|
149
|
+
name: string;
|
|
150
|
+
posts: { title: string; tags: string[] }[];
|
|
275
151
|
};
|
|
276
152
|
};
|
|
277
153
|
|
|
278
|
-
type
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
154
|
+
type T1 = GetType<Data, 'user.name'>; // string
|
|
155
|
+
type T2 = GetType<Data, 'user.posts'>; // { title: string; tags: string[] }[]
|
|
156
|
+
type T3 = GetType<Data, 'user.posts[*].title'>; // string
|
|
157
|
+
type T4 = GetType<Data, 'user.posts[*].tags'>; // string[]
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### `DeepDateToString<T>`
|
|
161
|
+
|
|
162
|
+
Recursively converts all `Date` types to `string`. Useful for representing serialized/JSON response types.
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
type Input = {
|
|
166
|
+
createdAt: Date;
|
|
167
|
+
user: { name: string; birthDate: Date };
|
|
289
168
|
};
|
|
290
169
|
|
|
291
|
-
|
|
292
|
-
|
|
170
|
+
type Output = DeepDateToString<Input>;
|
|
171
|
+
// { createdAt: string; user: { name: string; birthDate: string } }
|
|
172
|
+
```
|
|
293
173
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
};
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
*/
|
|
174
|
+
### `DeepStrictUnbrand<T>`
|
|
175
|
+
|
|
176
|
+
Recursively removes branding (e.g., `typia` tags like `Format<'uuid'>`) from types, restoring base primitives.
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
type Branded = {
|
|
180
|
+
id: string & { __brand: 'uuid' };
|
|
181
|
+
profile: { email: string & { __brand: 'email' } };
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
type Clean = DeepStrictUnbrand<Branded>;
|
|
185
|
+
// { id: string; profile: { email: string } }
|
|
310
186
|
```
|
|
311
187
|
|
|
312
|
-
|
|
188
|
+
## Runtime Functions
|
|
189
|
+
|
|
190
|
+
### `deepStrictObjectKeys(obj)`
|
|
313
191
|
|
|
314
|
-
|
|
192
|
+
Runtime counterpart of `DeepStrictObjectKeys`. Returns an array of all dot-notation key paths.
|
|
315
193
|
|
|
316
|
-
|
|
317
|
-
|
|
194
|
+
```typescript
|
|
195
|
+
import { deepStrictObjectKeys } from '@kakasoo/deep-strict-types';
|
|
318
196
|
|
|
319
|
-
|
|
197
|
+
const keys = deepStrictObjectKeys({ a: { b: 1, c: 2 } });
|
|
198
|
+
// ["a", "a.b", "a.c"]
|
|
199
|
+
```
|
|
320
200
|
|
|
321
|
-
|
|
322
|
-
- **Ensures Type Consistency:** By explicitly converting `Date` to `string`, it prevents type mismatches during serialization or API responses.
|
|
323
|
-
- **Handles Complex Structures:** Works reliably even with deeply nested objects and arrays containing `Date` values.
|
|
201
|
+
### `deepStrictAssert(obj)(key)`
|
|
324
202
|
|
|
325
|
-
|
|
203
|
+
Curried runtime function that extracts a specific nested property, preserving the object structure. Type-safe counterpart of `DeepStrictPick`.
|
|
326
204
|
|
|
327
205
|
```typescript
|
|
328
|
-
|
|
329
|
-
type Example = {
|
|
330
|
-
createdAt: Date;
|
|
331
|
-
updatedAt: Date;
|
|
332
|
-
user: {
|
|
333
|
-
name: string;
|
|
334
|
-
birthDate: Date;
|
|
335
|
-
posts: {
|
|
336
|
-
title: string;
|
|
337
|
-
publishedAt: Date;
|
|
338
|
-
}[];
|
|
339
|
-
};
|
|
340
|
-
};
|
|
206
|
+
import { deepStrictAssert } from '@kakasoo/deep-strict-types';
|
|
341
207
|
|
|
342
|
-
|
|
343
|
-
|
|
208
|
+
const data = {
|
|
209
|
+
user: { name: 'Alice', age: 30 },
|
|
210
|
+
posts: [{ title: 'Hello', content: 'World' }],
|
|
211
|
+
};
|
|
344
212
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
{
|
|
348
|
-
createdAt: string;
|
|
349
|
-
updatedAt: string;
|
|
350
|
-
user: {
|
|
351
|
-
name: string;
|
|
352
|
-
birthDate: string;
|
|
353
|
-
posts: {
|
|
354
|
-
title: string;
|
|
355
|
-
publishedAt: string;
|
|
356
|
-
}[];
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
*/
|
|
213
|
+
const result = deepStrictAssert(data)('user.name');
|
|
214
|
+
// { user: { name: 'Alice' } }
|
|
360
215
|
```
|
|
361
216
|
|
|
362
|
-
|
|
217
|
+
## Utility Types
|
|
218
|
+
|
|
219
|
+
| Type | Description | Example |
|
|
220
|
+
|------|-------------|---------|
|
|
221
|
+
| `DeepStrictObjectLastKeys<T>` | Extracts only the leaf-level (deepest) keys | `"a.b.c"` instead of `"a" \| "a.b" \| "a.b.c"` |
|
|
222
|
+
| `StringToDeepObject<T>` | Converts a comma-separated dot-notation string to a nested object type | `StringToDeepObject<"a.b,c">` = `{ a: { b: any }; c: any }` |
|
|
223
|
+
| `Equal<X, Y>` | Type-level equality check (returns `true` or `false`) | `Equal<string, string>` = `true` |
|
|
224
|
+
| `ElementOf<T>` | Extracts the element type from an array | `ElementOf<string[]>` = `string` |
|
|
225
|
+
| `IsAny<T>` | Checks if a type is `any` | `IsAny<any>` = `true` |
|
|
226
|
+
| `IsUnion<T>` | Checks if a type is a union | `IsUnion<string \| number>` = `true` |
|
|
227
|
+
| `ValueType` | Union of all primitive types + `Date` | `string \| number \| boolean \| ...` |
|
|
228
|
+
| `GetMember<T, Prefix>` | Extracts key segments after a dot-notation prefix | Internal helper for `DeepStrictOmit` |
|
|
229
|
+
| `GetElementMember<T, Prefix>` | Extracts array element sub-keys after a `[*]` prefix | Internal helper for `DeepStrictOmit` |
|
|
230
|
+
| `RemoveAfterDot<T, K>` | Generates wildcard patterns for descendant keys | Internal helper for `DeepStrictPick` |
|
|
231
|
+
| `RemoveArraySymbol<T>` | Strips `[*]` suffix from a key string | `RemoveArraySymbol<"items[*]">` = `"items"` |
|
|
232
|
+
| `RemoveLastProperty<T>` | Extracts all parent path segments | `RemoveLastProperty<"a.b.c">` = `"a" \| "a.b"` |
|
|
233
|
+
|
|
234
|
+
## License
|
|
235
|
+
|
|
236
|
+
ISC
|
|
@@ -1,4 +1,23 @@
|
|
|
1
1
|
import { DeepStrictObjectKeys } from '../types/DeepStrictObjectKeys';
|
|
2
2
|
import { DeepStrictPick } from '../types/DeepStrictPick';
|
|
3
|
+
/**
|
|
4
|
+
* @title Runtime Function for Type-Safe Deep Property Extraction.
|
|
5
|
+
*
|
|
6
|
+
* A curried function that takes an object and returns a picker function.
|
|
7
|
+
* The picker function accepts a dot-notation key path and returns a new object
|
|
8
|
+
* containing only the specified nested property, preserving the original structure.
|
|
9
|
+
*
|
|
10
|
+
* This is the runtime counterpart of the {@link DeepStrictPick} type.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The object type of the input
|
|
13
|
+
* @param input - The source object to extract properties from
|
|
14
|
+
* @returns A function that accepts a key path `K` and returns the deeply-picked result
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const result = deepStrictAssert({ a: { b: 1, c: 2 } })('a.b');
|
|
19
|
+
* // result: { a: { b: 1 } }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
3
22
|
export declare const deepStrictAssert: <T extends object>(input: T) => <K extends DeepStrictObjectKeys<T>>(key: K) => DeepStrictPick<T, K>;
|
|
4
23
|
//# sourceMappingURL=DeepStrictAssert.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DeepStrictAssert.d.ts","sourceRoot":"","sources":["../../../src/functions/DeepStrictAssert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEzD,eAAO,MAAM,gBAAgB,GAC1B,CAAC,SAAS,MAAM,SAAS,CAAC,MAC1B,CAAC,SAAS,oBAAoB,CAAC,CAAC,CAAC,OAAO,CAAC,KAAG,cAAc,CAAC,CAAC,EAAE,CAAC,CAiC/D,CAAC"}
|
|
1
|
+
{"version":3,"file":"DeepStrictAssert.d.ts","sourceRoot":"","sources":["../../../src/functions/DeepStrictAssert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAEzD;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,gBAAgB,GAC1B,CAAC,SAAS,MAAM,SAAS,CAAC,MAC1B,CAAC,SAAS,oBAAoB,CAAC,CAAC,CAAC,OAAO,CAAC,KAAG,cAAc,CAAC,CAAC,EAAE,CAAC,CAiC/D,CAAC"}
|
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.deepStrictAssert = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @title Runtime Function for Type-Safe Deep Property Extraction.
|
|
6
|
+
*
|
|
7
|
+
* A curried function that takes an object and returns a picker function.
|
|
8
|
+
* The picker function accepts a dot-notation key path and returns a new object
|
|
9
|
+
* containing only the specified nested property, preserving the original structure.
|
|
10
|
+
*
|
|
11
|
+
* This is the runtime counterpart of the {@link DeepStrictPick} type.
|
|
12
|
+
*
|
|
13
|
+
* @template T - The object type of the input
|
|
14
|
+
* @param input - The source object to extract properties from
|
|
15
|
+
* @returns A function that accepts a key path `K` and returns the deeply-picked result
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const result = deepStrictAssert({ a: { b: 1, c: 2 } })('a.b');
|
|
20
|
+
* // result: { a: { b: 1 } }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
4
23
|
const deepStrictAssert = (input) => (key) => {
|
|
5
24
|
const keys = key.split(/(?:\[\*\])?\./g).filter(Boolean);
|
|
6
25
|
const traverse = (input, keys) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DeepStrictAssert.js","sourceRoot":"","sources":["../../../src/functions/DeepStrictAssert.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"DeepStrictAssert.js","sourceRoot":"","sources":["../../../src/functions/DeepStrictAssert.ts"],"names":[],"mappings":";;;A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C,KAAK,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,CAAC;YAED,MAAM,IAAI,KAAK,CAAC,2BAA2B,KAAK,EAAE,CAAC,CAAC;QACtD,CAAC;IACH,CAAC,CAAC;IAEF,OAAO,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAyB,CAAC;AACvD,CAAC,CAAC;AAnCS,QAAA,gBAAgB,oBAmCzB"}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { DeepStrictObjectKeys } from '../types';
|
|
2
|
+
/** @internal Removes a leading dot from a string type. e.g., `".foo"` becomes `"foo"`. */
|
|
2
3
|
type RemoveStartWithDot<T extends string> = T extends `.${infer R extends string}` ? R : T;
|
|
4
|
+
/** @internal Replaces all `[*]` array notation with `${number}` for runtime key path access. */
|
|
3
5
|
type Replace<S extends string> = S extends '[*]' ? `${number}` : S extends `[*].${infer Rest}` ? `${number}.${Replace<Rest>}` : S extends `${infer Prefix extends string}.[*]${infer Rest}` ? `${Prefix}.${number}${Replace<Rest>}` : S extends `${infer Prefix extends string}[*]${infer Rest}` ? `${Prefix}.${number}${Replace<Rest>}` : S;
|
|
6
|
+
/**
|
|
7
|
+
* @internal The return type for {@link deepStrictObjectKeys}.
|
|
8
|
+
* Converts type-level keys (with `[*]` notation) to runtime-friendly keys (with `${number}` notation),
|
|
9
|
+
* strips leading dots, and wraps the result in an array type.
|
|
10
|
+
*/
|
|
4
11
|
type ReturnType<Target extends object, Joiner extends {
|
|
5
12
|
array: string;
|
|
6
13
|
object: string;
|
|
@@ -8,6 +15,26 @@ type ReturnType<Target extends object, Joiner extends {
|
|
|
8
15
|
array: '[*]';
|
|
9
16
|
object: '.';
|
|
10
17
|
}> = [Target] extends [never] ? [] : RemoveStartWithDot<Replace<DeepStrictObjectKeys<Target, Joiner, false>>>[];
|
|
18
|
+
/**
|
|
19
|
+
* @title Runtime Function for Extracting All Nested Keys from an Object.
|
|
20
|
+
*
|
|
21
|
+
* Recursively traverses the input object and returns a flat array of all key paths
|
|
22
|
+
* using dot notation. Nested objects produce paths like `"a.b.c"`, and arrays produce
|
|
23
|
+
* indexed paths at runtime.
|
|
24
|
+
*
|
|
25
|
+
* This is the runtime counterpart of the {@link DeepStrictObjectKeys} type.
|
|
26
|
+
*
|
|
27
|
+
* @template Target - The object type to extract keys from
|
|
28
|
+
* @template Joiner - Separator symbols (defaults to `{ array: '[*]', object: '.' }`)
|
|
29
|
+
* @param target - The object instance to extract keys from
|
|
30
|
+
* @returns An array of all dot-notation key paths in the object
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const keys = deepStrictObjectKeys({ a: { b: 1, c: 2 } });
|
|
35
|
+
* // keys: ["a", "a.b", "a.c"]
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
11
38
|
export declare function deepStrictObjectKeys<Target extends object, Joiner extends {
|
|
12
39
|
array: string;
|
|
13
40
|
object: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DeepStrictObjectKeys.d.ts","sourceRoot":"","sources":["../../../src/functions/DeepStrictObjectKeys.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAEhD,KAAK,kBAAkB,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;AAE3F,KAAK,OAAO,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,KAAK,GAC5C,GAAG,MAAM,EAAE,GACX,CAAC,SAAS,OAAO,MAAM,IAAI,EAAE,GAC3B,GAAG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC,EAAE,GAC5B,CAAC,SAAS,GAAG,MAAM,MAAM,SAAS,MAAM,OAAO,MAAM,IAAI,EAAE,GACzD,GAAG,MAAM,IAAI,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,EAAE,GACrC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ime Function for Extracting All Nested Keys from an Object.
|
|
6
|
+
*
|
|
7
|
+
* Recursively traverses the input object and returns a flat array of all key paths
|
|
8
|
+
* using dot notation. Nested objects produce paths like `"a.b.c"`, and arrays produce
|
|
9
|
+
* indexed paths at runtime.
|
|
10
|
+
*
|
|
11
|
+
* This is the runtime counterpart of the {@link DeepStrictObjectKeys} type.
|
|
12
|
+
*
|
|
13
|
+
* @template Target - The object type to extract keys from
|
|
14
|
+
* @template Joiner - Separator symbols (defaults to `{ array: '[*]', object: '.' }`)
|
|
15
|
+
* @param target - The object instance to extract keys from
|
|
16
|
+
* @returns An array of all dot-notation key paths in the object
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const keys = deepStrictObjectKeys({ a: { b: 1, c: 2 } });
|
|
21
|
+
* // keys: ["a", "a.b", "a.c"]
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
4
24
|
function deepStrictObjectKeys(target) {
|
|
5
25
|
let joiner = { array: '[*]', object: '.' };
|
|
6
26
|
if (joiner === undefined) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DeepStrictObjectKeys.js","sourceRoot":"","sources":["../../../src/functions/DeepStrictObjectKeys.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"DeepStrictObjectKeys.js","sourceRoot":"","sources":["../../../src/functions/DeepStrictObjectKeys.ts"],"names":[],"mappings":";;AA8CA,oDAwBC;AA5CD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,oBAAoB,CAGlC,MAAc;IACd,IAAI,MAAM,GAAW,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAY,CAAC;IAC7D,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,MAAM,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAY,CAAC;IACnD,CAAC;IAED,MAAM,QAAQ,GAAG,EAAE,CAAC;IACpB,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACjC,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;IAEvB,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,GAAG,IAAI,MAAM,EAAE,CAAC;YAClB,MAAM,KAAK,GAAI,MAAc,CAAC,GAAG,CAAC,CAAC;YACnC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBAChD,MAAM,QAAQ,GAAG,oBAAoB,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,GAAG,IAAI,EAAE,EAAE,CAAC,CAAC;gBACzE,QAAQ,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC;YAC7B,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,QAAQ,CAAC,CAA+B,CAAC;AACrE,CAAC"}
|