I attempted to extend the String.prototype
with a new method, but I encountered an issue.
interface String {
newMethod(): void
}
String.prototype.newMethod = function() {}
Although there were no errors in the typescriptlang.org playground
, I received the error message
Property 'newMethod' does not exist on type 'String'
when testing it on my local computer.
I am puzzled by this inconsistency.
Below is an excerpt from my tsconfig.json
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"outDir": "./lib",
"rootDir": "./src",
}
}
I also made sure to install `@types/node
Upon further research, I came across some interesting examples.
// example1: no error
interface String {
newMethod(): void
}
String.prototype.newMethod = function() {}
// example2: has error
import * as path from 'path'
interface String {
newMethod(): void
}
String.prototype.newMethod = function() {}
The addition of only an import statement seemed to trigger the error. This behavior is quite baffling to me. Any insights?