Currently, I am in the process of developing a function that retrieves data from an API. Having a clear idea of what the data will be according to the API path, I opted for using 'ApiSignature' types.
Here is a simplified version of my code:
interface Order {
id: string
}
type ApiSignature<P, D> = {
path: P,
data: D
};
type OrdersApiSignature = ApiSignature<'/orders', Order[]>
type OrderApiSignature = ApiSignature<'/order', Order>
type AnyApiSignature = OrdersApiSignature | OrderApiSignature
function useApi<T extends AnyApiSignature>(path: T['path']) {
return null as any as T['data']
}
const data = useApi('/orders') // Order | Order[]
Could TypeScript potentially infer that in this scenario the data will be Order[]
rather than just Order
, based on the function argument?