Mașina de scris tip de definiție pentru funcția împachetări asincron funcție

0

Problema

Am o functie are nevoie de un arbitrar asincron funcție și returnează rezultatul de așteptarea că asincron funcție, dar înfășurat într-un try/catch care se adaugă unele suplimentare de logică. A se vedea ts loc de joacă.

const with401Redirection =
    <T extends (...args: any[]) => Promise<any>>(
        call: T
    ): ((...args: Parameters<T>) => ReturnType<T>) =>
    // @ts-expect-error
    async (...args: Parameters<T>): ReturnType<T> => {
        try {
            return await call(...args);
        } catch (error) {
            if ((error as any).httpStatus === 401) {
                // do some stuff here
            }

            throw error;
        }
    };

interface User {
    id: string;
    name: string;
}

interface ItemPayload {
    field1: string;
    field2: string;
}

interface ItemResponse {
    id: string;
    field1: string;
    field2: string;
}

const client = {
    get<ResponseType>(url: string): Promise<ResponseType> {
        // logic to hit server and return result here
        return '' as any;
    },
    post<ResponseType>(url: string, body: Record<string, any>): Promise<ResponseType> {
        // logic to hit server and return result here
        return '' as any;
    }
};

const getUser = with401Redirection(() =>
    client.get<User>('url_1')
);

const saveItem = with401Redirection((body: ItemPayload) =>
    client.post<ItemResponse>('url_2', body)
);

Mă simt ca // @ts-expect-error în with401Redirection nu ar trebui să fie necesar-cum pot elimina sau, în general, curat tastarea de with401Redirection funcția? Păstrați în minte, vreau să susțin faptul că getUser și saveItem funcțiile lor tipuri automat dedusă pentru mine.

1

Cel mai bun răspuns

2

Încercați acest lucru:

TS teren de Joacă pentru link

type Awaited<T> = T extends PromiseLike<infer U> ? Awaited<U> : T;
type AsyncFn = (...args: any[]) => Promise<any>;

function with401Redirection <T extends AsyncFn>(call: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {
    return async (...args: Parameters<T>) => {
        try {
            return await call(...args);
        }
        catch (exception) {
            if (typeof exception === 'object' && (exception as any)?.httpStatus === 401) {
                // do some stuff here
            }
            throw exception;
        }
    };
}

Citiți despre cele actuale, viitoare Awaited tip în TS 4.5:

https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#the-awaited-type-and-promise-improvements

2021-11-13 00:21:15

În alte limbi

Această pagină este în alte limbi

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................