3

Property 'toString' does not exist on type 'number'.

I hate asking this question, I imagine it is a very simple setting or typings thing that this fresh project is missing. The fact that it will run (via ts-node) tells me it is just VS Code complaining (but, why?) Google and SO searches turned up nothing concrete yet.

Visual Studio Code
Version 1.22.2
Commit 3aeede733d9a3098f7b4bdc1f66b63b0f48c1ef9
Date 2018-04-12T16:38:45.278Z
Shell 1.7.12
Renderer 58.0.3029.110
Node 7.9.0
Architecture x64


> node -v
v9.10.0


> npm -v
5.6.0


> tsc -v
Version 2.8.3


> npm ls --depth=0
api-auth-test@1.0.0 C:\...
+-- @types/node@9.6.6
+-- axios@0.18.0
`-- typescript@2.8.3


> npm ls --depth=0 -g
C:\Users\...\AppData\Roaming\npm
+-- generator@1.0.1
+-- generator-gitignore@0.1.2
+-- npx@10.2.0
+-- ts-node@6.0.1
+-- tslint@5.9.1
+-- typescript@2.8.3
`-- yo@2.0.2

I found mention here of the tsdk setting in preferences, settings, and I ensured that typescript is installed into the local node_modules

"typescript.tsdk": "node_modules\\typescript\\lib\\",

Editing this, and the error disappears briefly, then returns (as VS refreshing its cache I imagine.)

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "lib": [ ],                               /* Specify library files to be included in the compilation. */
    "sourceMap": true,                        /* Generates corresponding '.map' file. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "esModuleInterop": true                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  }
}

package.json

{
  "name": "api-auth-test",
  "version": "1.0.0",
  "description": "",
  "main": "app.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^9.6.6",
    "axios": "~0.18.0",
    "typescript": "^2.8.3"
  },
  "devDependencies": {}
}
t.j.
  • 1,227
  • 3
  • 16
  • 30

2 Answers2

1

Your tsconfig lib is missing the es2015 entry or rather is empty.

"lib": [
    "dom",
    "es2015"
],

Removing es2015 gave me the error you are facing. For information on --lib option see What does the tsconfig option "lib" do?

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • How utterly maddening, I tried about every permutation of options for lib (including "dom" **and** "es6"), but the error wouldn't go away. I did as you suggested, **boom** problem solved! A million thanks for the fix and especially the link. I haven't seen it mentioned specifically but, am I to understand they are not "additive"? Should adding just "es6" gave **not** been sufficient? Or is "dom" the more critical piece for this specific `toString` issue? – t.j. Apr 26 '18 at 22:17
  • 1
    "dom" is the runtime environment you target. It has its own set pf api. Maybe https://basarat.gitbooks.io/typescript/docs/types/lib.d.ts.html will help – Suraj Rao Apr 27 '18 at 04:52
  • Ha, indeed! "This code type checks fine because the toString function is defined in lib.d.ts for all JavaScript objects.", and then the very error I was looking for. EXCELLENT find! – t.j. Apr 27 '18 at 06:14
0

I have observed similar issues with Angular 8 based solutions.These are set up with "es2018", after adding also "es2015" the red squigglies went away.

   {
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "es2015",
      "dom"
    ]
  }
}

Now, it might be the case that you should not mix these libs and just stick to one of these. But the point was to underline that Angular 8 uses "es2018" and I observed that adding "es2015" also fixed up some seemingly wrong errors that were fine when running inside the browser (false positives).

Tore Aurstad
  • 3,189
  • 1
  • 27
  • 22