@@ -5,6 +5,7 @@ import type { DtsGenerationConfig, DtsGenerationOption } from '@stacksjs/dtsx'
5
5
import { existsSync } from 'node:fs'
6
6
import { resolve } from 'node:path'
7
7
8
+ // Testing some randomness
8
9
/**
9
10
* Example of const declaration
10
11
*/
@@ -240,7 +241,7 @@ export * from './generate'
240
241
export * from './types'
241
242
export * from './utils'
242
243
243
- // 1. Complex Generic Types
244
+ // Complex Generic Types
244
245
export interface ComplexGeneric < T extends Record < string , unknown > , K extends keyof T > {
245
246
data : T
246
247
key : K
@@ -249,15 +250,15 @@ export interface ComplexGeneric<T extends Record<string, unknown>, K extends key
249
250
nested : Array < Partial < T > >
250
251
}
251
252
252
- // 2. Intersection and Union Types
253
+ // Intersection and Union Types
253
254
export type ComplexUnionIntersection =
254
255
| ( User & { role : 'admin' } )
255
256
| ( Product & { category : string } )
256
257
& {
257
258
metadata : Record < string , unknown >
258
259
}
259
260
260
- // 3. Mapped and Conditional Types
261
+ // Mapped and Conditional Types
261
262
export type ReadonlyDeep < T > = {
262
263
readonly [ P in keyof T ] : T [ P ] extends object ? ReadonlyDeep < T [ P ] > : T [ P ]
263
264
}
@@ -268,7 +269,7 @@ export type ConditionalResponse<T> = T extends Array<infer U>
268
269
? ApiResponse < T >
269
270
: ApiResponse < string >
270
271
271
- // 4. Complex Function Overloads
272
+ // Complex Function Overloads
272
273
export function processData ( data : string ) : string
273
274
export function processData ( data : number ) : number
274
275
export function processData ( data : boolean ) : boolean
@@ -277,23 +278,182 @@ export function processData(data: unknown): unknown {
277
278
return data
278
279
}
279
280
280
- // 5. Nested Object Types with Methods
281
- export const complexObject = {
282
- handlers : {
283
- async onSuccess < T > ( data : T ) : Promise < void > {
284
- console . log ( data )
285
- } ,
286
- onError ( error : Error & { code ?: number } ) : never {
287
- throw error
288
- }
289
- } ,
290
- utils : {
291
- formatters : {
292
- date : ( input : Date ) => input . toISOString ( ) ,
293
- currency : ( amount : number , currency = 'USD' ) =>
294
- new Intl . NumberFormat ( 'en-US' , { style : 'currency' , currency } ) . format ( amount )
295
- }
296
- }
281
+ export type EventType = 'click' | 'focus' | 'blur'
282
+ export type ElementType = 'button' | 'input' | 'form'
283
+ export type EventHandler = `on${Capitalize < EventType > } ${Capitalize < ElementType > } `
284
+
285
+ // Recursive Types
286
+ export type RecursiveObject = {
287
+ id : string
288
+ children ?: RecursiveObject [ ]
289
+ parent ?: RecursiveObject
290
+ metadata : Record < string , unknown >
291
+ }
292
+
293
+ // Complex Arrays and Tuples
294
+ export const complexArrays = {
295
+ matrix : [
296
+ [ 1 , 2 , [ 3 , 4 , [ 5 , 6 ] ] ] ,
297
+ [ 'a' , 'b' , [ 'c' , 'd' ] ] ,
298
+ [ true , [ false , [ true ] ] ] ,
299
+ ] ,
300
+ tuples : [
301
+ [ 1 , 'string' , true ] as const ,
302
+ [ 'literal' , 42 , false ] as const ,
303
+ ] ,
304
+ // TODO: get this part to generate correctly
305
+ // mixedArrays: [
306
+ // new Date(),
307
+ // Promise.resolve('async'),
308
+ // async () => 'result',
309
+ // function* generator() { yield 42 },
310
+ // ]
311
+ }
312
+
313
+ // TODO: Nested Object Types with Methods
314
+ // export const complexObject = {
315
+ // handlers: {
316
+ // async onSuccess<T>(data: T): Promise<void> {
317
+ // console.log(data)
318
+ // },
319
+ // onError(error: Error & { code?: number }): never {
320
+ // throw error
321
+ // }
322
+ // },
323
+ // utils: {
324
+ // formatters: {
325
+ // date: (input: Date) => input.toISOString(),
326
+ // currency: (amount: number, currency = 'USD') =>
327
+ // new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(amount)
328
+ // }
329
+ // }
330
+ // }
331
+
332
+ // TODO: Default Type Parameters
333
+ // export interface DefaultGeneric<
334
+ // T = string,
335
+ // K extends keyof any = string,
336
+ // V extends Record<K, T> = Record<K, T>
337
+ // > {
338
+ // key: K
339
+ // value: T
340
+ // record: V
341
+ // }
342
+
343
+ // TODO: Method Decorators and Metadata
344
+ // export const methodDecorator = (
345
+ // target: any,
346
+ // propertyKey: string,
347
+ // descriptor: PropertyDescriptor
348
+ // ) => {
349
+ // return {
350
+ // ...descriptor,
351
+ // enumerable: true,
352
+ // }
353
+ // }
354
+
355
+ // Complex Async Patterns
356
+ // due to isolatedDeclarations, we can assume the return type here
357
+ // export async function* complexAsyncGenerator(): any {
358
+ // const results = await Promise.all([
359
+ // fetchUsers(),
360
+ // getProduct(1),
361
+ // authenticate('user', 'pass'),
362
+ // ])
363
+
364
+ // for (const result of results) {
365
+ // yield result
366
+ // }
367
+ // }
368
+
369
+ // Type Assertions and Guards
370
+ // export function isUser(value: unknown): value is User {
371
+ // return (
372
+ // typeof value === 'object'
373
+ // && value !== null
374
+ // && 'id' in value
375
+ // && 'email' in value
376
+ // )
377
+ // }
378
+
379
+ // Branded Types
380
+ export type UserId = string & { readonly __brand : unique symbol }
381
+ export type ProductId = number & {
382
+ readonly __brand : unique symbol
383
+ }
384
+
385
+ // TODO: Complex Error Handling
386
+ // export class CustomError extends Error {
387
+ // constructor(
388
+ // message: string,
389
+ // public readonly code: number,
390
+ // public readonly metadata: Record<string, unknown>
391
+ // ) {
392
+ // super(message)
393
+ // this.name = 'CustomError'
394
+ // }
395
+ // }
396
+
397
+ // TODO: Module Augmentation
398
+ // declare module '@stacksjs/dtsx' {
399
+ // interface DtsGenerationConfig {
400
+ // customPlugins?: Array<{
401
+ // name: string
402
+ // transform: (code: string) => string
403
+ // }>
404
+ // }
405
+ // }
406
+
407
+ // Utility Type Implementations
408
+ export type DeepPartial < T > = T extends object ? {
409
+ [ P in keyof T ] ?: DeepPartial < T [ P ] >
410
+ } : T
411
+
412
+ export type DeepRequired < T > = T extends object ? {
413
+ [ P in keyof T ] -?: DeepRequired < T [ P ] >
414
+ } : T
415
+
416
+ // TODO: Complex Constants with Type Inference
417
+ // export const CONFIG_MAP = {
418
+ // development: {
419
+ // features: {
420
+ // auth: {
421
+ // providers: ['google', 'github'] as const,
422
+ // settings: { timeout: 5000, retries: 3 }
423
+ // }
424
+ // }
425
+ // },
426
+ // production: {
427
+ // features: {
428
+ // auth: {
429
+ // providers: ['google', 'github', 'microsoft'] as const,
430
+ // settings: { timeout: 3000, retries: 5 }
431
+ // }
432
+ // }
433
+ // }
434
+ // } as const
435
+
436
+ // Polymorphic Types
437
+ export type PolymorphicComponent < P = { } > = {
438
+ < C extends React . ElementType > (
439
+ props : { as ?: C } & Omit < React . ComponentPropsWithRef < C > , keyof P > & P
440
+ ) : React . ReactElement | null
441
+ }
442
+
443
+ // TODO: Type Inference in Functions
444
+ // export function createApi<T extends Record<string, (...args: any[]) => any>>(
445
+ // endpoints: T
446
+ // ): { [K in keyof T]: ReturnType<T[K]> extends Promise<infer R> ? R : ReturnType<T[K]> } {
447
+ // return {} as any
448
+ // }
449
+
450
+ // Complex Index Types
451
+ export type DynamicRecord < K extends PropertyKey > = {
452
+ [ P in K ] : P extends number
453
+ ? Array < unknown >
454
+ : P extends string
455
+ ? Record < string , unknown >
456
+ : never
297
457
}
298
458
299
459
export default dts
0 commit comments