Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 28, 2024
1 parent 5326f73 commit d274699
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 27 deletions.
202 changes: 181 additions & 21 deletions fixtures/input/example-0001.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { DtsGenerationConfig, DtsGenerationOption } from '@stacksjs/dtsx'
import { existsSync } from 'node:fs'
import { resolve } from 'node:path'

// Testing some randomness
/**
* Example of const declaration
*/
Expand Down Expand Up @@ -240,7 +241,7 @@ export * from './generate'
export * from './types'
export * from './utils'

// 1. Complex Generic Types
// Complex Generic Types
export interface ComplexGeneric<T extends Record<string, unknown>, K extends keyof T> {
data: T
key: K
Expand All @@ -249,15 +250,15 @@ export interface ComplexGeneric<T extends Record<string, unknown>, K extends key
nested: Array<Partial<T>>
}

// 2. Intersection and Union Types
// Intersection and Union Types
export type ComplexUnionIntersection =
| (User & { role: 'admin' })
| (Product & { category: string })
& {
metadata: Record<string, unknown>
}

// 3. Mapped and Conditional Types
// Mapped and Conditional Types
export type ReadonlyDeep<T> = {
readonly [P in keyof T]: T[P] extends object ? ReadonlyDeep<T[P]> : T[P]
}
Expand All @@ -268,7 +269,7 @@ export type ConditionalResponse<T> = T extends Array<infer U>
? ApiResponse<T>
: ApiResponse<string>

// 4. Complex Function Overloads
// Complex Function Overloads
export function processData(data: string): string
export function processData(data: number): number
export function processData(data: boolean): boolean
Expand All @@ -277,23 +278,182 @@ export function processData(data: unknown): unknown {
return data
}

// 5. Nested Object Types with Methods
export const complexObject = {
handlers: {
async onSuccess<T>(data: T): Promise<void> {
console.log(data)
},
onError(error: Error & { code?: number }): never {
throw error
}
},
utils: {
formatters: {
date: (input: Date) => input.toISOString(),
currency: (amount: number, currency = 'USD') =>
new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(amount)
}
}
export type EventType = 'click' | 'focus' | 'blur'
export type ElementType = 'button' | 'input' | 'form'
export type EventHandler = `on${Capitalize<EventType>}${Capitalize<ElementType>}`

// Recursive Types
export type RecursiveObject = {
id: string
children?: RecursiveObject[]
parent?: RecursiveObject
metadata: Record<string, unknown>
}

// Complex Arrays and Tuples
export const complexArrays = {
matrix: [
[1, 2, [3, 4, [5, 6]]],
['a', 'b', ['c', 'd']],
[true, [false, [true]]],
],
tuples: [
[1, 'string', true] as const,
['literal', 42, false] as const,
],
// TODO: get this part to generate correctly
// mixedArrays: [
// new Date(),
// Promise.resolve('async'),
// async () => 'result',
// function* generator() { yield 42 },
// ]
}

// TODO: Nested Object Types with Methods
// export const complexObject = {
// handlers: {
// async onSuccess<T>(data: T): Promise<void> {
// console.log(data)
// },
// onError(error: Error & { code?: number }): never {
// throw error
// }
// },
// utils: {
// formatters: {
// date: (input: Date) => input.toISOString(),
// currency: (amount: number, currency = 'USD') =>
// new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(amount)
// }
// }
// }

// TODO: Default Type Parameters
// export interface DefaultGeneric<
// T = string,
// K extends keyof any = string,
// V extends Record<K, T> = Record<K, T>
// > {
// key: K
// value: T
// record: V
// }

// TODO: Method Decorators and Metadata
// export const methodDecorator = (
// target: any,
// propertyKey: string,
// descriptor: PropertyDescriptor
// ) => {
// return {
// ...descriptor,
// enumerable: true,
// }
// }

// Complex Async Patterns
// due to isolatedDeclarations, we can assume the return type here
// export async function* complexAsyncGenerator(): any {
// const results = await Promise.all([
// fetchUsers(),
// getProduct(1),
// authenticate('user', 'pass'),
// ])

// for (const result of results) {
// yield result
// }
// }

// Type Assertions and Guards
// export function isUser(value: unknown): value is User {
// return (
// typeof value === 'object'
// && value !== null
// && 'id' in value
// && 'email' in value
// )
// }

// Branded Types
export type UserId = string & { readonly __brand: unique symbol }
export type ProductId = number & {
readonly __brand: unique symbol
}

// TODO: Complex Error Handling
// export class CustomError extends Error {
// constructor(
// message: string,
// public readonly code: number,
// public readonly metadata: Record<string, unknown>
// ) {
// super(message)
// this.name = 'CustomError'
// }
// }

// TODO: Module Augmentation
// declare module '@stacksjs/dtsx' {
// interface DtsGenerationConfig {
// customPlugins?: Array<{
// name: string
// transform: (code: string) => string
// }>
// }
// }

// Utility Type Implementations
export type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>
} : T

export type DeepRequired<T> = T extends object ? {
[P in keyof T]-?: DeepRequired<T[P]>
} : T

// TODO: Complex Constants with Type Inference
// export const CONFIG_MAP = {
// development: {
// features: {
// auth: {
// providers: ['google', 'github'] as const,
// settings: { timeout: 5000, retries: 3 }
// }
// }
// },
// production: {
// features: {
// auth: {
// providers: ['google', 'github', 'microsoft'] as const,
// settings: { timeout: 3000, retries: 5 }
// }
// }
// }
// } as const

// Polymorphic Types
export type PolymorphicComponent<P = {}> = {
<C extends React.ElementType>(
props: { as?: C } & Omit<React.ComponentPropsWithRef<C>, keyof P> & P
): React.ReactElement | null
}

// TODO: Type Inference in Functions
// export function createApi<T extends Record<string, (...args: any[]) => any>>(
// endpoints: T
// ): { [K in keyof T]: ReturnType<T[K]> extends Promise<infer R> ? R : ReturnType<T[K]> } {
// return {} as any
// }

// Complex Index Types
export type DynamicRecord<K extends PropertyKey> = {
[P in K]: P extends number
? Array<unknown>
: P extends string
? Record<string, unknown>
: never
}

export default dts
40 changes: 34 additions & 6 deletions fixtures/output/example-0001.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,41 @@ export declare function processData(data: number): number;
export declare function processData(data: boolean): boolean;
export declare function processData<T extends object>(data: T): T;
export declare function processData(data: unknown): unknown;
export declare const complexObject: {
handlers: <T>(data: T) => Promise<void> {
console.log(data)
},
onError(error: Error & { code?: number }): never {
throw error;
export declare type EventType = 'click' | 'focus' | 'blur';
export declare type ElementType = 'button' | 'input' | 'form';
export declare type EventHandler = `on${Capitalize<EventType>}${Capitalize<ElementType>}`;
export declare type RecursiveObject = {
id: string
children?: RecursiveObject[]
parent?: RecursiveObject
metadata: Record<string, unknown>
}
export declare const complexArrays: {
matrix: Array<Array<1 | 2 | Array<3 | 4 | Array<5 | 6>>> | Array<'a' | 'b' | Array<'c' | 'd'>> | Array<true | Array<false | Array<true>>>>;
tuples: Array<Array<1 | 'string' | true> | Array<'literal' | 42 | false>>;
};
export declare type UserId = string & { readonly __brand: unique symbol };
export declare type ProductId = number & {
readonly __brand: unique symbol
}
export declare type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>
} : T
export declare type DeepRequired<T> = T extends object ? {
[P in keyof T]-?: DeepRequired<T[P]>
} : T
export declare type PolymorphicComponent<P = {}> = {
<C extends React.ElementType>(
props: { as?: C } & Omit<React.ComponentPropsWithRef<C>, keyof P> & P
): React.ReactElement | null
}
export type DynamicRecord<K extends PropertyKey> = {
[P in K]: P extends number
? Array<unknown>
: P extends string
? Record<string, unknown>
: never
}

export { generate, dtsConfig }
export type { DtsGenerationOption }
Expand Down

0 comments on commit d274699

Please sign in to comment.