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.
If the TypeScript compiler fails to locate this configuration file, you would get an error. But you can provide settings enlisted in this file through the equivalent command-line options which we will cover in the next lesson. So what does this file contain and what exactly it controls?
The solution to make the type error go away is to assure the TypeScript compiler that at run-time, the finally () method is going to be available (either natively or via a polyfill). Head over to your project's tsconfig.json file and add the value "es2018.promise" to the "lib" array:
For example, you can use this with the listFiles option to display the files without creating a build. By default, the TypeScript compiler emits JavaScript files ( .js ), source-map files ( .map ), and declaration files ( .d.ts) even if it encounters some compilation errors.
Therefor . and ./src will form a single virtual directory and TypeScript will compile the program just fine. Even for the rootDirs option, the TypeScript compiler does not change the module path in the compiled code or change the location of the module files in the output files.
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.
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"
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With