RyanRoll/react-use-api

[Feature request] Add Typescript generics

Closed this issue · 2 comments

Hi, the library works great for me. However, I just had a thought that we could benefit by being able to define the expected API response. For example:

interface OrderDetailsApiReponse {
    subtotal: number;
    total: number;
    items: ...
}

export const OrderPage: React.FC<PropsInterface> = (props) => {
    let [order, {loading, error}] = useApi<OrderDetailsApiReponse>('/api/order/1234');

    // order variable has type of OrderDetailsApiReponse
}

TypeScript would produce errors if you try to use variable that won't be in the response. It would also allow sharing interfaces between back-end and front-end if both use typescript.

I would be happy to submit a PR to add it.

That's a good point! I should have supported a custom interface, but I missed it. I'll add an enhancement soon.

Close this as the enhancement has been released.