@@ -80,6 +80,36 @@ const post = postIds.generate(); // "post-ab12cd34"
80
80
const session = sessionIds .generate (); // "session_a1b2c3d4e5f67890"
81
81
```
82
82
83
+ ### Type Inference with InferId
84
+
85
+ You can use the ` InferId ` utility type to extract the generated ID type from your ` IdHelper ` instances:
86
+
87
+ ``` typescript
88
+ import { IdHelper , type InferId } from " typed-id" ;
89
+
90
+ // Create ID helper instances
91
+ const userIdHelper = new IdHelper (" user" );
92
+ const orderIdHelper = new IdHelper (" order" , { separator: " ::" });
93
+
94
+ // Infer the types that would be generated
95
+ type UserId = InferId <typeof userIdHelper >; // "user_${string}"
96
+ type OrderId = InferId <typeof orderIdHelper >; // "order::${string}"
97
+
98
+ // Use the inferred types in your application
99
+ function processUser(id : UserId ) {
100
+ // id is guaranteed to be a user ID with the correct format
101
+ console .log (` Processing user: ${id } ` );
102
+ }
103
+
104
+ // This will work
105
+ const userId = userIdHelper .generate ();
106
+ processUser (userId ); // ✅ Type-safe
107
+
108
+ // This would cause a TypeScript error
109
+ const orderId = orderIdHelper .generate ();
110
+ // processUser(orderId); // ❌ Type error: Argument of type 'OrderId' is not assignable to parameter of type 'UserId'
111
+ ```
112
+
83
113
## 🔍 Zod Integration
84
114
85
115
If you're using Zod for validation, typed-id provides built-in schema creators:
@@ -153,6 +183,47 @@ type Options<S extends string | undefined = undefined> = {
153
183
type GeneratedID <P extends string , S extends string > = ` ${P }${S }${string } ` ;
154
184
```
155
185
186
+ #### ` InferId<T> `
187
+
188
+ A utility type that infers the generated ID type from an ` IdHelper ` instance type. This is useful for type-level programming and ensuring type consistency across your application.
189
+
190
+ ``` typescript
191
+ type InferId <T extends IdHelper <string , string >> = T extends IdHelper <
192
+ infer P ,
193
+ infer S
194
+ >
195
+ ? GeneratedId <P , SeparatorOrDefault <S >>
196
+ : never ;
197
+ ```
198
+
199
+ ** Usage Example:**
200
+
201
+ ``` typescript
202
+ import { IdHelper , InferId } from " typed-id" ;
203
+
204
+ // Create ID helper instances
205
+ const userIdHelper = new IdHelper (" user" );
206
+ const orderIdHelper = new IdHelper (" order" , { separator: " ::" });
207
+
208
+ // Infer the types that would be generated
209
+ type UserId = InferId <typeof userIdHelper >; // "user_${string}"
210
+ type OrderId = InferId <typeof orderIdHelper >; // "order::${string}"
211
+
212
+ // Use the inferred types in your application
213
+ function processUser(id : UserId ) {
214
+ // id is guaranteed to be a user ID with the correct format
215
+ console .log (` Processing user: ${id } ` );
216
+ }
217
+
218
+ // This will work
219
+ const userId = userIdHelper .generate ();
220
+ processUser (userId ); // ✅ Type-safe
221
+
222
+ // This would cause a TypeScript error
223
+ const orderId = orderIdHelper .generate ();
224
+ // processUser(orderId); // ❌ Type error: Argument of type 'OrderId' is not assignable to parameter of type 'UserId'
225
+ ```
226
+
156
227
## 🛠️ Development
157
228
158
229
### Prerequisites
0 commit comments