Amanaemonesia
Amanaemonesia

Reputation: 1

Comparing types of arguments passed to a function

Suppose we have the following abstractions to retrieve data from API:

We want to use this as follows:

const item = new DataItem<ItemDto>()

async function request (): Promise<ItemDto> { ... }

queryData(
    () => request()
    item
)

Is it possible to do the following without generic queryData function:

  1. Check that the result of the function fn matches the type stored in item?
  2. If we pass a function transformResponse, can we check that its return value matches item type?
  3. If we pass a function transformResponse, can we check that its argument value is the same as the type returned from the function fn?

Upvotes: 0

Views: 66

Answers (1)

NoBullsh1t
NoBullsh1t

Reputation: 686

No. A generic type variable is exactly the tool that will allow you to do this. The following should meet your requirements:

function queryData<T>(
  fn: () => Promise<T>,
  item: DataItem<T>,
  transformResponse: (value: Promise<T>) => DataItem<T>
)

Upvotes: 1

Related Questions