0

I've seen this question and other posts like it, and even done exactly what I wanted in the TS playground, but in VS I get red underlines and build errors.

enter image description here enter image description here

error txt: Error TS2339 Build:Property 'contains' does not exist on type 'String'.

Is there some configuration I'm missing?

Edit to illustrate comments:

enter image description here

Josh Gust
  • 4,102
  • 25
  • 41
  • Please include code and errors as text, not as images. – Heretic Monkey Oct 09 '19 at 18:08
  • Also, [string already has `includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes)... – Heretic Monkey Oct 09 '19 at 18:09
  • I included them as images so that one could **see** the location of the errors. I'll add the text description of them too. Also it looks like my ignorance is showing and should use `includes` for this, but would still like to know the answer. – Josh Gust Oct 09 '19 at 18:18
  • there is no `includes` method on string. – Josh Gust Oct 09 '19 at 18:34
  • The link in my comment points to the documentation for that method, and has links to the standards where it was defined. TypeScript depends on updated type definitions; perhaps you're using an older version of TypeScript? – Heretic Monkey Oct 09 '19 at 18:38
  • Still, is there an answer to the question being asked? Why can't I extend built-in types as indicated in the linked question? – Josh Gust Oct 09 '19 at 20:47

3 Answers3

1

Concerning your first error

TS2339 Build:Property 'contains' does not exist on type 'String'

make sure to extend the String interface in the global scope, not in a module file. That means, there may not be any export or import at the file top-level.

interface String {
    foo(): void
}

String.prototype.foo = () => { console.log("bar") }

"aksljflasd".foo()

Playground

You could also use declare global in a module file:

export {}

declare global {
    interface String {
        foo(): void
    }
}

"aksljflasd".foo()

Playground

Concerning second error

Property includes does not exist on type 'string'

you need minimum ES6 for String.prototype.includes. So it is likely, that you have target of ES5 or lower in tsconfig.json. Also, when you manually set lib option, make sure to have ES2015.Core included.

Playground

ford04
  • 66,267
  • 20
  • 199
  • 171
0

It depends from your project tsconfig file. This is full specification - tsconfig. Take a closer look at --lib option. Generally in order to have functionalities from different ECMAScript specs it needs to be defined in there.

Take a look on the same issue - no includes in string. So looks like compilation needs to have es2016 lib added (or newer spec):

{
    "compilerOptions": {
          // other options
          "lib": [
            "es2019" // any above or equal es2016
          ]
    },
Maciej Sikora
  • 19,374
  • 4
  • 49
  • 50
0

The example you posted works until you make an es6 module out of it. Try to use export or import somewhere in the playground file and watch the errors pop up.

The cleanest solution imho is to move the String interface declaration to a *.d.ts file and include it in the typescript compilation. I put the file into arbitrary /types folder and in my tsconfig.json use "include": ["types"] so that it gets picked up. Then you can perform the actual extension String.prototype.contains = ... in your code anywhere you like.

Dan Macak
  • 16,109
  • 3
  • 26
  • 43