/** * Function utility types for TypeScript * @module FunctionTypes */ /** * Extracts the return type of a function type * @template T - The function type * @example * ```typescript * type MyFunction = () => string; * type ReturnType = ReturnType; // string * * type AsyncFunction = () => Promise; * type AsyncReturnType = ReturnType; // Promise * ``` */ export type ReturnType any> = T extends (...args: any) => infer R ? R : any; /** * Extracts the parameter types of a function type * @template T - The function type * @example * ```typescript * type MyFunction = (name: string, age: number) => void; * type Params = Parameters; // [string, number] * * type NoParams = () => void; * type NoParamsType = Parameters; // [] * ``` */ export type Parameters any> = T extends (...args: infer P) => any ? P : never; /** * Extracts the first parameter type of a function * @template T - The function type * @example * ```typescript * type MyFunction = (name: string, age: number) => void; * type FirstParam = FirstParameter; // string * * type NoParams = () => void; * type NoFirstParam = FirstParameter; // never * ``` */ export type FirstParameter any> = Parameters[0]; /** * Extracts the last parameter type of a function * @template T - The function type * @example * ```typescript * type MyFunction = (name: string, age: number) => void; * type LastParam = LastParameter; // number * * type NoParams = () => void; * type NoLastParam = LastParameter; // never * ``` */ export type LastParameter any> = Parameters extends [...any[], infer L] ? L : never; /** * Creates a function type that takes no parameters * @template R - The return type * @example * ```typescript * type NoParamsFunction = NoParamsFunction; // () => string * ``` */ export type NoParamsFunction = () => R; /** * Creates a function type that takes a single parameter * @template T - The parameter type * @template R - The return type * @example * ```typescript * type StringFunction = SingleParamFunction; // (param: string) => number * ``` */ export type SingleParamFunction = (param: T) => R; /** * Creates a function type that takes two parameters * @template T1 - The first parameter type * @template T2 - The second parameter type * @template R - The return type * @example * ```typescript * type MathFunction = TwoParamFunction; // (a: number, b: number) => number * ``` */ export type TwoParamFunction = (param1: T1, param2: T2) => R; /** * Creates a function type that takes any number of parameters * @template T - The parameter types * @template R - The return type * @example * ```typescript * type VariadicFunction = VariadicFunction; // (...args: (string | number)[]) => boolean * ``` */ export type VariadicFunction = (...args: T[]) => R; /** * Makes a function type's parameters optional * @template T - The function type * @example * ```typescript * type RequiredFunction = (name: string, age: number) => void; * type OptionalFunction = OptionalParameters; // (name?: string, age?: number) => void * ``` */ export type OptionalParameters any> = T extends (...args: infer P) => infer R ? (...args: { [K in keyof P]?: P[K]; }) => R : never; /** * Creates a function type that returns void * @template T - The parameter types * @example * ```typescript * type VoidFunction = VoidFunction; // (a: string, b: number) => void * ``` */ export type VoidFunction = (...args: T) => void; /** * Creates a function type that returns a Promise * @template T - The parameter types * @template R - The resolved type * @example * ```typescript * type AsyncFunction = AsyncFunction; // (param: string) => Promise * ``` */ export type AsyncFunction = (...args: T) => Promise; /** * Extracts the type of a method from an object type * @template T - The object type * @template K - The method key * @example * ```typescript * interface User { * getName(): string; * getAge(): number; * } * * type GetNameMethod = MethodType; // () => string * ``` */ export type MethodType = T[K] extends (...args: any[]) => any ? T[K] : never; /** * Creates a function type that can be called with or without parameters * @template T - The parameter types * @template R - The return type * @example * ```typescript * type FlexibleFunction = FlexibleFunction; // (param?: string) => number * ``` */ export type FlexibleFunction = (param?: T) => R; //# sourceMappingURL=function.d.ts.map