V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
yangblink

问题一个 typescript 类型包裹问题

  •  
  •   yangblink · Jan 25, 2024 · 2040 views
    This topic created in 834 days ago, the information mentioned may be changed or developed.
    1 replies    2024-01-25 21:58:35 +08:00
    Opportunity
        1
    Opportunity  
       Jan 25, 2024
    这种?
    ```
    export type Client = typeof client;
    export type ApiCall<T> = (client: Client) => Promise<T>;
    export function useApi() {
    return async function <T extends {
    data: any;
    error?: any;
    response: Response;
    }>(apiCall: ApiCall<T>): Promise<Awaited<ReturnType<ApiCall<T>>>['data']> {
    const response = await apiCall(client);
    return response.data;
    };
    }

    client.GET('/url2')
    const api = useApi();

    const r = await api(client => client.GET('/url2'))
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   959 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 21:05 · PVG 05:05 · LAX 14:05 · JFK 17:05
    ♥ Do have faith in what you're doing.