属性 'trimLeft' 在类型 'string' 上不存在。图书馆:["dom", "es2018"]

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

当 运行 以下代码

时,我收到此错误
let foo = '  foo  '
console.log(foo.trimLeft())
//foo.trimStart() works neither

我知道互联网上的大多数解决方案都说,我必须修复我的 tsconfig.json 以包含 es20whatever。

有趣的是,我可以使用像 Promise.prototype.finally 和 rest spread 等 es2018 的东西。VSCode 也自动完成 trimStart() 对我来说很奇怪,因为项目和编辑器应该使用相同的 tsconfig.json。 但是这段代码无法编译。

这是我的 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"]
    }
  }
}

我 运行 这个在 monorepo angular 文件夹中(如上所示)。 也许这有问题,我不知道。

我不认为 es2018 会起作用。尝试将其更改为 "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"]
    }
  }
}

包括库 "es2019.string"。如果没有这样的库,请更新您的 Typescript 副本。它是相当新的,在提出这个问题时并不存在。

将打字稿库更新为"typescript":“~3.6.2”。在 tsconfig.json 库的数组中包含 "es2019"。它会起作用。

"target": "es2015",
"typeRoots": [
  "node_modules/@types"
],
"lib": [
  "es2018",
  "es2019",
  "dom"
]