UNPKG

types-belt

Version:

A comprehensive collection of TypeScript utility types for building robust and maintainable applications

153 lines 5.06 kB
/** * 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<MyFunction>; // string * * type AsyncFunction = () => Promise<number>; * type AsyncReturnType = ReturnType<AsyncFunction>; // Promise<number> * ``` */ export type ReturnType<T extends (...args: any) => 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<MyFunction>; // [string, number] * * type NoParams = () => void; * type NoParamsType = Parameters<NoParams>; // [] * ``` */ export type Parameters<T extends (...args: any) => 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<MyFunction>; // string * * type NoParams = () => void; * type NoFirstParam = FirstParameter<NoParams>; // never * ``` */ export type FirstParameter<T extends (...args: any) => any> = Parameters<T>[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<MyFunction>; // number * * type NoParams = () => void; * type NoLastParam = LastParameter<NoParams>; // never * ``` */ export type LastParameter<T extends (...args: any) => any> = Parameters<T> 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>; // () => string * ``` */ export type NoParamsFunction<R = any> = () => 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<string, number>; // (param: string) => number * ``` */ export type SingleParamFunction<T = any, R = any> = (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<number, number, number>; // (a: number, b: number) => number * ``` */ export type TwoParamFunction<T1 = any, T2 = any, R = any> = (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<string | number, boolean>; // (...args: (string | number)[]) => boolean * ``` */ export type VariadicFunction<T = any, R = any> = (...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<RequiredFunction>; // (name?: string, age?: number) => void * ``` */ export type OptionalParameters<T extends (...args: any) => 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<string, number>; // (a: string, b: number) => void * ``` */ export type VoidFunction<T extends any[] = any[]> = (...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<string, number>; // (param: string) => Promise<number> * ``` */ export type AsyncFunction<T extends any[] = any[], R = any> = (...args: T) => Promise<R>; /** * 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<User, 'getName'>; // () => string * ``` */ export type MethodType<T, K extends keyof T> = 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<string, number>; // (param?: string) => number * ``` */ export type FlexibleFunction<T = any, R = any> = (param?: T) => R; //# sourceMappingURL=function.d.ts.map