Property 'trimLeft' does not exist on type 'string'. Lib: ["dom", "es2018"]
Asked Answered
U

3

11

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.

Upi answered 30/1, 2019 at 10:24 Comment(3)
is it compile-time error or run-time error?Ambages
Compile time error. I can run it perfectly on the browser consoleUpi
Interesting, because when I got the "Property 'trimRight' does not exist on type 'string'." error adding es2018 to the lib array was enough to solve my problem.Pivotal
P
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.

Prefiguration answered 3/6, 2019 at 21:41 Comment(0)
O
12

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"
]
Oswald answered 29/8, 2019 at 9:17 Comment(1)
Interesting, because for me, adding es2018 to the lib array did the job.Pivotal
I
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"]
    }
  }
}
Ietta answered 30/1, 2019 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.