#How to handle generic types does not satisfy X

19 messages · Page 1 of 1 (latest)

mighty egret
#

Hey guys,
I am trying to use generics (I am fairly new to TS) and I am facing an issue:

`Type 'T' does not satisfy the constraint 'DocumentSchema[]``

What does that mean exactly and how to fix this?

Thanks!

chilly zodiac
#

MultiSearchResponse can only take elements that are DocumentSchema[]

#

but the function you wrote accepts anything as T

#

and uses that same T, and passes it to MultiSearchResponse

#

you need to put the same constraint on the function as well

mighty egret
#

thanks for your reply, so do I need an intermediary time such as:
type TypesenseEmployeeDocument<T extends DocumentSchema> = T?

chilly zodiac
#
private async performMultiSearch<T extends DocumentSchema[]>(
  // ...
): Promise<MultiSearchResponse<T>> {
}
#

oh, wait

#

forget what I just said

mighty egret
#

that makes sense but then the problem goes up to the upper function, where T does not extend DocumentSchema[]

chilly zodiac
#

tldr. you could provide the generic type when calling your function

#

like so

performMultiSearch<Foo>([1, 2, 3], searchParams);
#

but the function would return a Promise<MultiSearchResponse<DocumentSchema[]>>

mighty egret
#

yes but performMultiSearch exptect also something that extends DocumentSchema[]

chilly zodiac
mighty egret
#

yes that's what I am wondering, how to make it a DocumentSchema[]

chilly zodiac
#

maybe you could post the relevant part of the code

#

not too sure how all the types come together