I'm a beginner for typescript
.
when I use Math.log10
, it gives me this warning.
How can I solve it?
And, when I use ts-node math.ts
, it give me an error:
TSError: ⨯ Unable to compile TypeScript
src/modules/math2.ts (6,15): Property 'log10' does not exist on type 'Math'. (2339)
Here is my tsconfig.json
:
{
"compilerOptions": {
"experimentalDecorators": true,
"allowJs": true,
"target": "es5"
}
}
Math.log10
is ECMAScript2015 (aka es6) extension; that's why babel complains when you ask to transpile it to es5. For those who faced the same problem: of course, you can do as OP did and change target to es6. But it will mask some other problems with es5, like Safari 9 not supporting Object.assign
. If you cannot change target to es6, the preferred way is to add a definition, for example:
es6.d.ts
declare interface Math {
log10(x: number): number;
}
This will work, because browsers do support the method.
Math.log10 is utility included in ES6. So you need to update the Target to ES6 or later. Make Sure your tsconfig.json contains the given value.
{
"target": "ES2016",
"module": "ES6",
}
And add below settings to tsconfig.json
{
"rootDir" : "./ts",
"outDir" : "./js"
}
Here we done !
hit "tsc" into the terminal.
Hope this can be helpful for you !
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