/** * Generic object type with string keys and values of type T. * * @since 0.0.2 * @template T - The type of values in the object * @example * ```typescript * const obj: ObjectOf = { a: 1, b: 2 }; * const obj2: ObjectOf = { name: "John", email: "john@example.com" }; * ``` */ export type ObjectOf = Record | { [key: string]: T; }; /** * A synchronous callback function type. * * @since 0.0.2 * @template T - The return type of the callback * @template Args - The argument types of the callback (default: empty array) * @example * ```typescript * const callback: Callback = () => 42; * const callback2: Callback = (num, str) => `${num}-${str}`; * ``` */ export interface Callback { (...args: Args): T; } /** * An asynchronous callback function type that returns a Promise. * * @since 0.0.2 * @template T - The resolved type of the Promise * @template Args - The argument types of the callback (default: empty array) * @example * ```typescript * const asyncCallback: AsyncCallback = async () => 42; * const asyncCallback2: AsyncCallback = async (id) => { * return await fetchUser(id); * }; * ``` */ export interface AsyncCallback { (...args: Args): Promise; } /** * A generator callback function type that returns a Generator. * * @since 0.0.5 * @template T - The yielded type of the Generator * @template Args - The argument types of the callback (default: empty array) * @example * ```typescript * const genCallback: GeneratorCallback = function* () { * yield 1; * yield 2; * yield 3; * }; * * const rangeGen: GeneratorCallback = function* (start, end) { * for (let i = start; i <= end; i++) { * yield i; * } * }; * ``` */ export interface GeneratorCallback { (...args: Args): Generator; } /** * An async generator callback function type that returns an AsyncGenerator. * * @since 0.0.5 * @template T - The yielded type of the AsyncGenerator * @template Args - The argument types of the callback (default: empty array) * @example * ```typescript * const asyncGenCallback: AsyncGeneratorCallback = async function* () { * yield await Promise.resolve(1); * yield await Promise.resolve(2); * }; * * const fetchPages: AsyncGeneratorCallback = async function* (url) { * let page = 1; * while (true) { * const data = await fetch(`${url}?page=${page}`).then(r => r.json()); * if (!data.length) break; * yield data; * page++; * } * }; * ``` */ export interface AsyncGeneratorCallback { (...args: Args): AsyncGenerator; } /** * Primitive JavaScript types. * * @since 0.0.2 * @example * ```typescript * const str: Primitive = "hello"; * const num: Primitive = 42; * const bool: Primitive = true; * const nul: Primitive = null; * const undef: Primitive = undefined; * ``` */ export type Primitive = string | number | boolean | null | undefined; /** * Object with string keys and primitive values. * Commonly used for flattened object representations. * * @since 0.0.2 * @example * ```typescript * const flatData: FlattenedPrimitive = { * "user.name": "John", * "user.age": 30, * "user.active": true, * }; * ``` */ export type FlattenedPrimitive = Record | { [key: string]: Primitive; }; /** * A generic object literal type that can hold any key-value pairs. * * This type is useful when you need to accept any object-like structure, * including plain objects, records, and class instances. It uses PropertyKey * to allow string, number, and symbol keys. * * @since 0.0.5 * @example * ```typescript * // Plain object * const config: LiteralObject = { host: "localhost", port: 3000 }; * * // Nested object * const user: LiteralObject = { * name: "John", * address: { city: "NYC", zip: "10001" } * }; * * // With symbol keys * const sym = Symbol("id"); * const data: LiteralObject = { [sym]: 123, name: "test" }; * ``` * * @example * ```typescript * // Function accepting any object * function processObject(obj: LiteralObject) { * return Object.keys(obj); * } * * processObject({ a: 1, b: 2 }); // ["a", "b"] * ``` */ export type LiteralObject = Record | { [key: string]: unknown; } | object; /** * A generic function type that can represent any callable function. * * This type is a safer alternative to the built-in `Function` type, * which is too broad and discouraged by TypeScript/ESLint. * It uses the existing `Callback` interface with `any` types for maximum flexibility. * * @since 0.0.5 * @example * ```typescript * // Regular function * const greet: LiteralFunction = (name: string) => `Hello, ${name}`; * * // Function with properties * const fn: LiteralFunction = () => {}; * fn.version = "1.0.0"; * * // Checking if value is a function * function isFunc(value: unknown): value is LiteralFunction { * return typeof value === "function"; * } * ``` */ export type LiteralFunction = Callback, any[]>; /** * A generic class/constructor type that can represent any instantiable class. * * This type is useful when you need to accept class constructors as arguments, * for dependency injection, factory patterns, or reflection-like operations. * * @since 0.0.5 * @template T - The instance type that the class produces (default: unknown) * @example * ```typescript * class User { * constructor(public name: string) {} * } * * // Accept any class constructor * function createInstance(Cls: LiteralClass, ...args: unknown[]): T { * return new Cls(...args); * } * * const user = createInstance(User, "John"); * ``` * * @example * ```typescript * // Registry of classes * const registry: Map = new Map(); * registry.set("User", User); * registry.set("Product", Product); * * // Get and instantiate * const UserClass = registry.get("User"); * if (UserClass) { * const instance = new UserClass(); * } * ``` */ export type LiteralClass = new (...args: any[]) => T; /** * A union type of all built-in primitive and object types in JavaScript/TypeScript. * * This type includes: * - Basic primitives (string, number, boolean, null, undefined, bigint, symbol) * - Date and RegExp * - File API types (File, FileList, Blob, FormData) * - URL and URLSearchParams * - Binary data types (ArrayBuffer, SharedArrayBuffer, DataView, TypedArrays) * - Fetch API types (Headers, Request, Response) * - Abort API (AbortController, AbortSignal) * - Stream API (ReadableStream, WritableStream, TransformStream) * - Event types (Event, CustomEvent, EventTarget) * - Observer types (MutationObserver, IntersectionObserver, ResizeObserver) * - Worker and messaging types (Worker, MessageChannel, MessagePort, BroadcastChannel) * - Generator types (Generator, AsyncGenerator) * - DOM types (Element, HTMLElement, Node, Document, Window) * - Error types (Error, TypeError, RangeError, SyntaxError, ReferenceError, EvalError, AggregateError, URIError) * * @since 0.0.6 * @example * ```typescript * // Use as a constraint for generic types * function isBuiltIn(value: T): value is T & BuiltInPrimitive { * return value instanceof Date || value instanceof RegExp || typeof value !== 'object'; * } * * // Use in conditional types * type ExtractBuiltIn = T extends BuiltInPrimitive ? T : never; * ``` */ export type BuiltInPrimitive = Primitive | bigint | symbol | Date | RegExp | File | FileList | URL | Blob | ArrayBuffer | SharedArrayBuffer | DataView | Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array | FormData | Headers | Request | Response | URLSearchParams | AbortController | AbortSignal | ReadableStream | WritableStream | TransformStream | Event | CustomEvent | EventTarget | MutationObserver | IntersectionObserver | ResizeObserver | Worker | MessageChannel | MessagePort | BroadcastChannel | Generator | AsyncGenerator | Element | HTMLElement | Node | Document | Window | Error | TypeError | RangeError | SyntaxError | ReferenceError | EvalError | AggregateError | URIError; /** * A function type that can have additional properties attached to it. * * This type combines a callable function with an object type, allowing * functions to have custom properties. Useful for creating functions with * metadata, configuration, or namespaced utilities. * * @since 0.0.6 * @template F - The function type (default: Callback) * @template O - The object type for additional properties (default: LiteralObject) * @example * ```typescript * // Function with version property * type VersionedFn = ExtendedFunction<() => void, { version: string }>; * const myFn: VersionedFn = Object.assign(() => {}, { version: "1.0.0" }); * myFn(); // callable * console.log(myFn.version); // "1.0.0" * ``` * * @example * ```typescript * // Utility namespace pattern * type StringUtils = ExtendedFunction< * (str: string) => string, * { uppercase: (s: string) => string; lowercase: (s: string) => string } * >; * * const strUtils: StringUtils = Object.assign( * (str: string) => str.trim(), * { * uppercase: (s: string) => s.toUpperCase(), * lowercase: (s: string) => s.toLowerCase(), * } * ); * ``` */ export type ExtendedFunction, O = LiteralObject> = F & O; /** * A deep partial type that correctly handles built-in types. * * Unlike the standard `Partial`, this type recursively makes all properties * optional while preserving the structure of built-in types like Map, Set, * WeakMap, WeakSet, Promise, WeakRef, FinalizationRegistry, and Arrays. * * Built-in primitives (Date, RegExp, TypedArrays, etc.) are preserved as-is. * Object types have their properties made optional recursively. * Functions with properties have their properties made optional. * * @since 0.0.6 * @template T - The type to make partially optional * @example * ```typescript * interface User { * name: string; * address: { * city: string; * zip: string; * }; * tags: Set; * } * * // All properties become optional, including nested ones * type PartialUser = PartialBuiltIn; * const user: PartialUser = { * name: "John", * // address and tags are optional * }; * ``` * * @example * ```typescript * // Built-in types are preserved correctly * type PartialMap = PartialBuiltIn>; * // Result: Map, PartialBuiltIn<{ value: number }>> * * type PartialPromise = PartialBuiltIn>; * // Result: Promise> * ``` */ export type PartialBuiltIn = T extends Map ? Map, PartialBuiltIn> : T extends WeakMap ? WeakMap, PartialBuiltIn> : T extends ReadonlyMap ? ReadonlyMap, PartialBuiltIn> : T extends Set ? Set> : T extends WeakSet ? WeakSet> : T extends ReadonlySet ? ReadonlySet> : T extends Promise ? Promise> : T extends WeakRef ? WeakRef> : T extends FinalizationRegistry ? FinalizationRegistry> : T extends BuiltInPrimitive ? T : T extends ExtendedFunction ? T extends ExtendedFunction ? F & { [K in keyof T]?: PartialBuiltIn; } : T : T extends LiteralObject ? { [K in keyof T]?: PartialBuiltIn; } : T extends Array ? Array> : T extends ReadonlyArray ? ReadonlyArray> : T;