I'm stumped, hopefully this simplified example explains it well enough:
I have a class instance holding data, and another composite class instance to render the data. The data class gives callbacks to UI class in order to get and set data properties. I'm stumped on the "set" too but I'll stick to "getVal" for now.
class Person {
private _data: DataObj
constructor() {
this._ui = new PersonUI({
getVal: (key) => this.getVal(key)
})
}
getVal<T extends keyof DataObj>( key: T ) {
return this._data[key]
}
}
class PersonUI {
constructor(arg: UIArg) {}
}
Typescript can infer the type of the _data value, but I don't know how to write an interface for UIArg that will keep that inferred type, all I could come up with was "any" ex:
interface UIArg {
getVal: <T extends keyof DataObj>(key: T) => any
}
Is there a way to write the interface to keep the inferred type?
Bonus Q: is there a way to write the Person.p.setVal method to use the inferred type too? Ex:
setVal<T extends keyof DataObj>(key: T, val: any) {
this._data[key] = val
}