Next.js Discord

Discord Forum

make a new method on string prototype globally available throughout the project

Unanswered
Schneider’s Smooth-fronted Caima… posted this in #help-forum
Open in Discord
Schneider’s Smooth-fronted CaimanOP
I am creating a new method on String,
declare global {
    interface String {
        toTitleCase(): string
    }
}

String.prototype.toTitleCase = function() {
  return this.split(' ')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
    .join(' ');
};

// example
// "hello world".toTitleCase() gives "Hello World"

Once defined, I don't want to import it explicitly in every file to use it,
How can i make it globally available throughout my next js 14 project?

0 Replies