I am familiar with the Kotlin extension function let
(and related) (cf. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/let.html).
I wanted to use something similar in TypeScript, using augmentation (or is it called declaration merging?). So I thought I can add the let
method to the Object
superclass.
How can I do it? I thought of something like the following, but that didn't work. Typescript doesn't seem to augment, but to solely use the following interface.
interface Object {
let<T, R>(block: (t: T) => R): R
}
(Object.prototype as any).let = function<T, R>(block: (t: T) => R) {
return block(this)
}
EDIT:
Test cases would be:
'42'.let(it => alert(it))
'foo'.let(it => it.toUpperCase())
(i. e. let
would be available on any objects, in this case strings)