#Function overloading with `lodash` throttle

2 messages · Page 1 of 1 (latest)

lone viper
#

Hello, Im trying to declare a function overload like so:

import type { HasRecursiveRelations } from '@/interfaces/recursiveRelations';
import { handleHttpError, HttpError } from '@/services/errorHandler';
import type { TreeNode } from '@riophae/vue-treeselect';
import { throttle } from 'lodash';

type OnSuccessCallback<T> = (data: HasRecursiveRelations<T>[]) => void;
type OnFailureCallback = (error: HttpError) => void;

type FetchTreeSelectOptions<T> = ((endpoint: string, params?: Record<string, any>, onSuccess?: OnSuccessCallback<T>, onFailure?: OnFailureCallback) => void)
    | ((endpoint: string, onSuccess?: OnSuccessCallback<T>, onFailure?: OnFailureCallback) => void);

interface UseTreeSelectReturn<T> {
    fetchTreeSelectOptions: FetchTreeSelectOptions<T>;
    getTreeSelectLabel: (node: TreeNode) => string
}

export function useTreeSelect<T = any>(debounce?: number): UseTreeSelectReturn<T> {
    function fetchOptions(endpoint: string, params?: Record<string, any> | OnSuccessCallback<T>, onSuccess?: OnSuccessCallback<T> | OnFailureCallback, onFailure?: OnFailureCallback): void {
        if (typeof params === 'function') {
            onFailure = onSuccess as OnFailureCallback;
            onSuccess = params as OnSuccessCallback<T>;
            params = {};
        }

        // ...
    }

    const fetchTreeSelectOptions = debounce ? throttle(fetchOptions, debounce) : fetchOptions;

    // ...

    return {
        fetchTreeSelectOptions,
        // ...
    };
}

but when I try to use fetchTreeSelectOptions I get the resulting declaration

const fetchTreeSelectOptions: (endpoint: string, arg1: ((Record<string, any> & OnSuccessCallback<App.Models.Listing.StoreCategory>) | undefined), arg2: ((OnSuccessCallback<App.Models.Listing.StoreCategory> & OnFailureCallback) | undefined), onFailure: (OnFailureCallback | undefined)) => void
late yewBOT
#
fmtod#1256

Preview:```ts
import {throttle} from "lodash"

export type HasRecursiveRelations<
T = any,
RecursiveKey extends string = "children"

= T & {
[K in RecursiveKey]: HasRecursiveRelations<
T,
RecursiveKey
[]
}

type OnSuccessCallback<T> = (
data: HasRecursiveRelations<T>[]
) => void
...```