I'm trying to create a function that takes two parameters, the first is the class reference and the second is a function with only one parameter that is an instance of the class reference and the output is a specific type. I tried the code below, but the typescript shows me an unknown type for p, even though I hope p was an instance of Pair.
interface Point{
x: number
y: number
}
class Pair{
key = 0
value = 0
}
function set<Class extends { new (): Type }, Type>(
inputClass: Class,
constructor: (o: Type) => Point) {
//implementation...
}
set(Pair, p => ({x: p.key, y: p.value}))
// Typescript says:
// (parameter) p: unknown
// Object is of type 'unknown'.(2571)
I wanted the typescript to know that p must be an instance of Pair
I was able to implement using java generics, but I still can't replicate in typescript. Follow the code:
public <K extends Pair, T extends Class<K>> void setRenderer(T classe, Function<K, Point> constructor){
//implementation
}