Hello, I would like some help about a "complex" TypeScript issue I have. I wan't to create a generic function which memoize a callback value (a getter) to be using the ThisType of the object it is used in. Here is a code example which makes that clearer. I don't even know if this is possible.
declare const makeDefinition: <O extends {}>(obj: O & ThisType<O>) => O;
// If want `unknown` to be `ThisType` of the calling object.
declare const memoized: <T>(compute: (obj: unknown) => T) => T;
const definition = makeDefinition({
firstName: 'John',
lastName: 'Doe',
// So `obj` in memoized is correctly typed to the current `ThisType`.
fullName1: memoized((obj) => `${obj.firstName} ${obj.lastName}`),
// Just like in the following getter.
get fullName2() {
return `${this.firstName} ${this.lastName}`;
},
});