10

I am getting this Error when running the following code

let foo = '  foo  '
console.log(foo.trimLeft())
//foo.trimStart() works neither

I know most of the solutions in the internet say, that I have to fix my tsconfig.json to include es20whatever.

The funny thing is, I can use es2018 stuff like Promise.prototype.finally and rest spread etc. VSCode also auto completes trimStart() for me which is odd, because the project and the editor should use the same tsconfig.json. But this particular piece of code does not compile.

Here is my tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "./",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2018", "dom"],
    "plugins": [
      {
        "name": "tslint-language-service",
        "configFile": "./tslint.json"
      }
    ],
    "paths": {
      "foo": ["projects/foo/src/public_api.ts"],
      "bar": ["projects/bar/src/public_api.ts"],
      "baz": ["dist/baz"]
    }
  }
}

I am running this in a monorepo angular folder (as you can see above). Perhaps there is an issue with that, I don't know.

uloco
  • 2,283
  • 4
  • 22
  • 37

3 Answers3

14

Include the library "es2019.string". Update your copy of Typescript if there is no such library. It is rather new and did not exist when this question was asked.

Mushinako
  • 312
  • 3
  • 9
11

Update the typescript library to "typescript": "~3.6.2". Include "es2019" in the tsconfig.json lib's array. It will work.

"target": "es2015",
"typeRoots": [
  "node_modules/@types"
],
"lib": [
  "es2018",
  "es2019",
  "dom"
]
mshahin364
  • 169
  • 2
  • 3
0

I don't think es2018 is going to work. Try changing it to "es2016"

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "./",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2016", "dom"],
    "plugins": [
      {
        "name": "tslint-language-service",
        "configFile": "./tslint.json"
      }
    ],
    "paths": {
      "foo": ["projects/foo/src/public_api.ts"],
      "bar": ["projects/bar/src/public_api.ts"],
      "baz": ["dist/baz"]
    }
  }
}
Melchia
  • 22,578
  • 22
  • 103
  • 117