I have developed a new interface for String that incorporates additional utility methods by utilizing Monkey-patching.
interface String {
toCamelCase(): string;
}
String.prototype.toCamelCase = function (): string {
return this.replace(/[^a-z ]/gi, '').replace(
/(?:^\w|[A-Z]|\b\w|\s+)/g,
(match: any, index: number) => {
return +match === 0
? ''
: match[index === 0 ? 'toLowerCase' : 'toUpperCase']();
},
);
};
When calling the new function in my controller, toCamelCase :
const str: string = 'this is an example';
const result = str.toCamelCase();
console.log(result);
An error arises with the following message:
[Nest] 35664 - ERROR [ExceptionsHandler] str.toCamelCase is not a function TypeError: str.toCamelCase is not a function
What might be causing this issue in the implementation?