windows - VS 代码属性 'toString' 在类型 'number' 上不存在

标签 windows typescript visual-studio-code

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

我讨厌问这个问题,我想这个新项目缺少一个非常简单的设置或输入内容。它将运行(通过 ts-node)这一事实告诉我这只是 VS Code 的提示(但是,为什么?)Google 和 SO 搜索还没有找到任何具体的结果。

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

我发现这里提到了 tsdk setting在首选项、设置中,我确保 typescript 安装到本地 node_modules

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

编辑它,错误会短暂消失,然后返回(我想 VS 会刷新其缓存。)

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": {}
}

最佳答案

您的 tsconfig lib 缺少 es2015 条目或者是空的。

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

删除 es2015 给了我你面临的错误。 有关 --lib 选项的信息,请参阅 What does the tsconfig option "lib" do?

关于windows - VS 代码属性 'toString' 在类型 'number' 上不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50040105/

相关文章:

visual-studio-code - 在 Visual Studio 代码中禁用 .ejs 文件的保存格式

javascript - 如何使用 webview 为 VSCode 扩展实现 React App 导航

c++ - 安装了全局 Hook 后,我如何知道窗口何时开始移动以及何时停止移动?

windows - 如何在 Vista 中使用 SetWindowsHookEx 并将管理应用程序与 UAC Hook ?

excel - 用于 64 位 Visual Basic 的更新

typescript 装饰二传手功能

angular - canActivate 上结束 return 语句的问题

c# - 带有图形的自定义字体样式

typescript - 如何在 TypeScript 中实现强类型 ID?

windows - Visual Studio Code 空白终端,无法输入/键入任何内容 [Windows 10]