Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript - Property 'log10' does not exist on type 'Math'?

Tags:

typescript

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)

enter image description here

Here is my tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "allowJs": true,
    "target": "es5"
  }
}
like image 861
slideshowp2 Avatar asked Apr 30 '17 03:04

slideshowp2


2 Answers

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.

like image 165
ob-ivan Avatar answered Oct 03 '22 07:10

ob-ivan


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",
}

This problem can be happen when you are compiling single file. So to avoid this, follow given steps.
Create 2 Directories at root level.
  1. (Which contains Source TypeScript) called ts (any name you can use).
  2. (Which store Compiled JavaScript) called js (any name you can use).

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 !

like image 26
Parag Bharadva Avatar answered Oct 03 '22 05:10

Parag Bharadva