Skip to content

Can not extend String interface #55931

Description

@jkalandarov

🔎 Search Terms

"typescript does not extend string interface"
"typescript does not augment string interface"
"typescript does not augment global string interface"

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgMpiqA5sg3gKGSOTAHsBhOAWwgBtKBnCACgEoAuZBjbfAX3z50mEFgB0AByikyYAJ4SIYspRr04TNsgC8yGAFcQCMMFIg8hYlAhh9Uc2AAWwBstIAZUgHdojFq0tiYjFrCVpEFgB6ZgB+dgA9AB0vAB8AbQBBAFoALQBdFMSAI2TWSKwAGj1DY1NzZi9SKAATKtBmiAAPVgsgoOtbe2R2rp1tXQAGZBjkRpa3Tx8oPy1OOea3AFUJRWWNfwBuQOQ+AL7g0PCkZkjEhgBqcqqAcmfWI74gA

💻 Code

global.d.ts:

declare global {
  interface String {
    toCamelCase(): string
  }
}

transformers.ts:

interface String {
  toCamelCase(): string
}

String.prototype.toCamelCase = function() {
  return this.toLowerCase()
        .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
        return index === 0 ? word.toLowerCase() : word.toUpperCase();
    })
    .replace(/\s+/g, '');
}

actions.ts:

When(/^I click on "([^"]*\sbutton|checkbox|radiobutton)" on "([^"]*)"/, function (element: string, page: string) {
  console.log('**** ', element.toCamelCase()) // <= Returns error
});

🙁 Actual behavior

image

🙂 Expected behavior

Transform string to camel case

Additional information about the issue

 "devDependencies": {
    "@cucumber/cucumber": "^9.5.1",
    "@playwright/test": "^1.38.0",
    "@types/node": "^20.8.0",
    "multiple-cucumber-html-reporter": "^3.5.0",
    "ts-node": "^10.9.1"
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    ExternalRelates to another program, environment, or user action which we cannot control.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions