【问题标题】:VS Code Property 'toString' does not exist on type 'number'VS Code 属性“toString”在“数字”类型上不存在
【发布时间】:2023-03-19 16:41:02
【问题描述】:

我讨厌问这个问题,我想这是一个非常简单的设置或打字的东西,这个新项目缺少。它将运行(通过ts-node)的事实告诉我,这只是 VS Code 的抱怨(但是,为什么?)Google 和 SO 搜索结果还没有具体。

Visual Studio 代码
版本 1.22.2
提交 3aeede733d9a3098f7b4bdc1f66b63b0f48c1ef9
日期 2018-04-12T16:38:45.278Z
壳牌 1.7.12
渲染器 58.0.3029.110
节点 7.9.0
架构 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": {}
}

【问题讨论】:

    标签: windows typescript visual-studio-code


    【解决方案1】:

    您的 tsconfig lib 缺少 es2015 条目,或者更确切地说是空的。

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

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

    【讨论】:

    • 多么令人抓狂,我尝试了 lib 选项的所有排列(包括“dom”“es6”),但错误不会消失。我按照你的建议做了,boom 问题解决了!一百万感谢修复,尤其是链接。我没有看到具体提到它,但是,我是否理解它们不是“添加剂”?是否应该只添加“es6”给 not 就足够了?还是“dom”对于这个特定的toString 问题更关键?
    • "dom" 是您定位的运行时环境。它有自己的一套 pf api。也许basarat.gitbooks.io/typescript/docs/types/lib.d.ts.html 会有所帮助
    • 哈,确实! “此代码类型检查良好,因为在 lib.d.ts 中为所有 JavaScript 对象定义了 toString 函数。”,然后是我正在寻找的错误。很棒的发现!
    【解决方案2】:

    我观察到基于 Angular 8 的解决方案存在类似问题。这些是使用“es2018”设置的,在添加“es2015”后,红色波浪线消失了。

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

    现在,您可能不应该混合使用这些库,而只使用其中之一。但重点是要强调 Angular 8 使用“es2018”,我观察到添加“es2015”还修复了一些看似错误的错误,这些错误在浏览器中运行时很好(误报)。

    【讨论】:

      猜你喜欢
      • 2018-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2017-03-02
      • 2021-09-07
      • 2020-08-02
      相关资源
      最近更新 更多