I would like to force the 2 parameters of a function to respect the name and the type of an object. So I am looking for the XXX type of the val parameter to be the type of the T[key]:
class I<T> {}
class W<T extends object> {
field<K extends keyof T>(key: K, val: XXX) { void 0;}
}
In order to have the following behavior:
class A {
i:number=3
s:string="test"
}
let Winstance:W<A>=new W<A>()
Winstance.field("i",new I<number>()) // OK
Winstance.field("s",new I<number>()) // NOK
Winstance.field("i",new I<string>()) //NOK
Winstance.field("s",new I<string>()) //OK
Is there any way? I tryed
class W<T extends object> {
field<K extends keyof T>(key: K, val: I<T[K]>) { void 0;}
}
But it doesn't work
Make a generic and use that key
Kto have a more specific selector forvalTS Playground