Let's say we have the following abstractions for fetching data from an API:
Data storage class
class DataItem<T> { data?: T | null }
Query function
function queryData ( fn: () => Promise<any> item: DataItem<any> transformResponse?: (value: any) => any // value - result from fn )
We intend to use these as shown below:
const item = new DataItem<ItemDto>()
async function request (): Promise<ItemDto> { ... }
queryData(
() => request()
item
)
Is there a way to achieve the following without using generic queryData
function:
- Validate that the output of function
fn
aligns with the type stored initem
? - If we provide a function
transformResponse
, can its return value be checked against the type ofitem
? - If we pass a function
transformResponse
, is it possible to ensure that its parametervalue
matches the type returned by functionfn
?