टाइपप्रति प्रकार परिभाषा समारोह के लिए लपेटता है कि async समारोह

0

सवाल

मैं एक समारोह लेता है लेता है एक मनमाने ढंग से async समारोह और रिटर्न के परिणाम का इंतजार कर रहा है कि async कार्य है, लेकिन एक में लिपटे प्रयास करें/catch कहते हैं, जो कुछ अतिरिक्त तर्क । देखें टीएस खेल का मैदान.

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)
);

मैं की तरह लग रहा है // @ts-expect-error में with401Redirection नहीं होना चाहिए आवश्यक-मैं कैसे कर सकते हैं इसे हटाने या आम तौर पर साफ टाइपिंग के with401Redirection समारोह? मन में रखने के लिए मैं चाहता हूँ बनाए रखने के लिए तथ्य यह है कि getUser और saveItem कार्यों के अपने प्रकार स्वचालित रूप से inferred के लिए मुझे.

1

सबसे अच्छा जवाब

2

इस प्रयास करें:

टीएस खेल का मैदान कड़ी

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;
        }
    };
}

पढ़ने के बारे में वास्तविक, आगामी Awaited प्रकार में टीएस 4.5:

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

2021-11-13 00:21:15

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में