0

Let's say I would like to add this method onto String:

String.prototype.frenchQuote = function() {
  return '« ' + this + ' »';
}

TypeScript will complain that there is no frenchQuote method on String.

Online, many suggestions can be found to then declare it like so:

interface String {
  frenchQuote(): string
}

However, TypeScript flags this interface as unused, and still complains about the non-existent method.

Is there a new way to augment the signature of imported types or built-ins?

Using TypeScript version 3.7

P Varga
  • 19,174
  • 12
  • 70
  • 108
  • 1
    you might just need to specify a namespace (global, window?) so it picks up the interface – pushkin Feb 06 '20 at 22:35
  • @VladGincher none of the answers are accepted there, and the most upvoted one doesn't seem to work in TypeScript 3.7 – P Varga Feb 06 '20 at 22:41

1 Answers1

0

Thank you @pushkin for the hint. The solution was to signal to TS that the interface is global:

declare global {
  interface String {
    frenchQuote(): string
  }
}
P Varga
  • 19,174
  • 12
  • 70
  • 108