First the code snippet I try to get to work:
// taken from https://github.com/TheDavidDelta/scope-extensions-js and https://stackoverflow.com/a/65808350/2080707
declare global {
/**
* let calls the specified function block with `this` value as its argument and returns its result
* @param fn - The function to be executed with `this` as argument
* @returns `fn`'s result
*/
interface Object {
let<R>(fn: (val: this) => R): R;
}
}
Object.defineProperty(Object.prototype, 'let', {
value(fn) {
return fn(this);
},
configurable: true,
writable: true,
});
And now what I try to achieve:
({ a: 'foobar' }).let((obj) => console.debug(obj.a))
Of course this example is contrived and doesn't make much sense. But there are some cases where it does.
My problem is that let's argument is of type object
and not of type {a:string}
.
For this I need to reference the object whichs prototype got extended and not object itself.
Any pointer would be helpful :pray:
Edit: One could adapt https://github.com/HerbLuo/babel-plugin-kotlish-also to be able to support let
as well. But it creates a lot of function overhead as well I guess.