0

why is when I attempt the code

declare interface String {
  testsdfsdffds();
}

String.prototype.testsdfsdffds = () => {

};

it works, but substituting Object for String causes errors. I thought you could extend Object without issues?

My goal really is to mimic the Kotlin apply and let extension functions - but I would like to understand the how/why you can/cant extend objects?

Brad
  • 280
  • 4
  • 7
  • What errors do you get? Could you briefly explain what are you trying to achieve (for those unfamiliar with Kotlin)? – Aleksey L. May 20 '18 at 04:49
  • 2
    No, [it doesn't cause an error](http://www.typescriptlang.org/play/#src=declare%20interface%20Object%20%7B%0D%0A%20%20testsdfsdffds()%3B%0D%0A%7D%0D%0A%0D%0AObject.prototype.testsdfsdffds%20%3D%20()%20%3D%3E%20%7B%0D%0A%0D%0A%7D%3B%0D%0A%0D%0A(%7B%7D).testsdfsdffds()). Consider providing https://stackoverflow.com/help/mcve that can replicate the problem. Any way, modified built-in prototypes have strong smell and should be avoided when possible, i.e. almost always. – Estus Flask May 20 '18 at 11:50

0 Answers0