|
| 1 | +/** |
| 2 | + * Extended test cases for DTS generation |
| 3 | + */ |
| 4 | + |
| 5 | +// 1. Complex Generic Types |
| 6 | +export interface ComplexGeneric<T extends Record<string, unknown>, K extends keyof T> { |
| 7 | + data: T |
| 8 | + key: K |
| 9 | + value: T[K] |
| 10 | + transform: (input: T[K]) => string |
| 11 | + nested: Array<Partial<T>> |
| 12 | +} |
| 13 | + |
| 14 | +// 2. Intersection and Union Types |
| 15 | +export type ComplexUnionIntersection = |
| 16 | + | (User & { role: 'admin' }) |
| 17 | + | (Product & { category: string }) |
| 18 | + & { |
| 19 | + metadata: Record<string, unknown> |
| 20 | + } |
| 21 | + |
| 22 | +// 3. Mapped and Conditional Types |
| 23 | +export type ReadonlyDeep<T> = { |
| 24 | + readonly [P in keyof T]: T[P] extends object ? ReadonlyDeep<T[P]> : T[P] |
| 25 | +} |
| 26 | + |
| 27 | +export type ConditionalResponse<T> = T extends Array<infer U> |
| 28 | + ? ApiResponse<U[]> |
| 29 | + : T extends object |
| 30 | + ? ApiResponse<T> |
| 31 | + : ApiResponse<string> |
| 32 | + |
| 33 | +// 4. Complex Function Overloads |
| 34 | +export function processData(data: string): string |
| 35 | +export function processData(data: number): number |
| 36 | +export function processData(data: boolean): boolean |
| 37 | +export function processData<T extends object>(data: T): T |
| 38 | +export function processData(data: unknown): unknown { |
| 39 | + return data |
| 40 | +} |
| 41 | + |
| 42 | +// 5. Nested Object Types with Methods |
| 43 | +export const complexObject = { |
| 44 | + handlers: { |
| 45 | + async onSuccess<T>(data: T): Promise<void> { |
| 46 | + console.log(data) |
| 47 | + }, |
| 48 | + onError(error: Error & { code?: number }): never { |
| 49 | + throw error |
| 50 | + } |
| 51 | + }, |
| 52 | + utils: { |
| 53 | + formatters: { |
| 54 | + date: (input: Date) => input.toISOString(), |
| 55 | + currency: (amount: number, currency = 'USD') => |
| 56 | + new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(amount) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// 6. Template Literal Types |
| 62 | +export type EventType = 'click' | 'focus' | 'blur' |
| 63 | +export type ElementType = 'button' | 'input' | 'form' |
| 64 | +export type EventHandler = `on${Capitalize<EventType>}${Capitalize<ElementType>}` |
| 65 | + |
| 66 | +// 7. Recursive Types |
| 67 | +export type RecursiveObject = { |
| 68 | + id: string |
| 69 | + children?: RecursiveObject[] |
| 70 | + parent?: RecursiveObject |
| 71 | + metadata: Record<string, unknown> |
| 72 | +} |
| 73 | + |
| 74 | +// 8. Complex Array Types |
| 75 | +export const complexArrays = { |
| 76 | + matrix: [ |
| 77 | + [1, 2, [3, 4, [5, 6]]], |
| 78 | + ['a', 'b', ['c', 'd']], |
| 79 | + [true, [false, [true]]], |
| 80 | + ], |
| 81 | + tuples: [ |
| 82 | + [1, 'string', true] as const, |
| 83 | + ['literal', 42, false] as const, |
| 84 | + ], |
| 85 | + mixedArrays: [ |
| 86 | + new Date(), |
| 87 | + Promise.resolve('async'), |
| 88 | + async () => 'result', |
| 89 | + function* generator() { yield 42 }, |
| 90 | + ] |
| 91 | +} |
| 92 | + |
| 93 | +// 9. Default Type Parameters |
| 94 | +export interface DefaultGeneric< |
| 95 | + T = string, |
| 96 | + K extends keyof any = string, |
| 97 | + V extends Record<K, T> = Record<K, T> |
| 98 | +> { |
| 99 | + key: K |
| 100 | + value: T |
| 101 | + record: V |
| 102 | +} |
| 103 | + |
| 104 | +// 10. Method Decorators and Metadata |
| 105 | +export const methodDecorator = ( |
| 106 | + target: any, |
| 107 | + propertyKey: string, |
| 108 | + descriptor: PropertyDescriptor |
| 109 | +) => { |
| 110 | + return { |
| 111 | + ...descriptor, |
| 112 | + enumerable: true, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// 11. Complex Async Patterns |
| 117 | +export async function* complexAsyncGenerator() { |
| 118 | + const results = await Promise.all([ |
| 119 | + fetchUsers(), |
| 120 | + getProduct(1), |
| 121 | + authenticate('user', 'pass'), |
| 122 | + ]) |
| 123 | + |
| 124 | + for (const result of results) { |
| 125 | + yield result |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +// 12. Type Assertions and Guards |
| 130 | +export function isUser(value: unknown): value is User { |
| 131 | + return ( |
| 132 | + typeof value === 'object' |
| 133 | + && value !== null |
| 134 | + && 'id' in value |
| 135 | + && 'email' in value |
| 136 | + ) |
| 137 | +} |
| 138 | + |
| 139 | +// 13. Branded Types |
| 140 | +export type UserId = string & { readonly __brand: unique symbol } |
| 141 | +export type ProductId = number & { readonly __brand: unique symbol } |
| 142 | + |
| 143 | +// 14. Complex Error Handling |
| 144 | +export class CustomError extends Error { |
| 145 | + constructor( |
| 146 | + message: string, |
| 147 | + public readonly code: number, |
| 148 | + public readonly metadata: Record<string, unknown> |
| 149 | + ) { |
| 150 | + super(message) |
| 151 | + this.name = 'CustomError' |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// 15. Module Augmentation |
| 156 | +declare module '@stacksjs/dtsx' { |
| 157 | + interface DtsGenerationConfig { |
| 158 | + customPlugins?: Array<{ |
| 159 | + name: string |
| 160 | + transform: (code: string) => string |
| 161 | + }> |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// 16. Utility Type Implementations |
| 166 | +export type DeepPartial<T> = T extends object ? { |
| 167 | + [P in keyof T]?: DeepPartial<T[P]> |
| 168 | +} : T |
| 169 | + |
| 170 | +export type DeepRequired<T> = T extends object ? { |
| 171 | + [P in keyof T]-?: DeepRequired<T[P]> |
| 172 | +} : T |
| 173 | + |
| 174 | +// 17. Complex Constants with Type Inference |
| 175 | +export const CONFIG_MAP = { |
| 176 | + development: { |
| 177 | + features: { |
| 178 | + auth: { |
| 179 | + providers: ['google', 'github'] as const, |
| 180 | + settings: { timeout: 5000, retries: 3 } |
| 181 | + } |
| 182 | + } |
| 183 | + }, |
| 184 | + production: { |
| 185 | + features: { |
| 186 | + auth: { |
| 187 | + providers: ['google', 'github', 'microsoft'] as const, |
| 188 | + settings: { timeout: 3000, retries: 5 } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} as const |
| 193 | + |
| 194 | +// 18. Polymorphic Types |
| 195 | +export type PolymorphicComponent<P = {}> = { |
| 196 | + <C extends React.ElementType>( |
| 197 | + props: { as?: C } & Omit<React.ComponentPropsWithRef<C>, keyof P> & P |
| 198 | + ): React.ReactElement | null |
| 199 | +} |
| 200 | + |
| 201 | +// 19. Type Inference in Functions |
| 202 | +export function createApi<T extends Record<string, (...args: any[]) => any>>( |
| 203 | + endpoints: T |
| 204 | +): { [K in keyof T]: ReturnType<T[K]> extends Promise<infer R> ? R : ReturnType<T[K]> } { |
| 205 | + return {} as any |
| 206 | +} |
| 207 | + |
| 208 | +// 20. Complex Index Types |
| 209 | +export type DynamicRecord<K extends PropertyKey> = { |
| 210 | + [P in K]: P extends number |
| 211 | + ? Array<unknown> |
| 212 | + : P extends string |
| 213 | + ? Record<string, unknown> |
| 214 | + : never |
| 215 | +} |
0 commit comments