TypeScript error: “Cannot find name” in Visual Studio

China☆狼群 提交于 2019-12-12 18:29:56

问题


I've seen a lot of threads and discussions about that but I'm not getting to fix the issue. This is my post from some days ago.

One of the purposes of typings is to avoid the use of <reference> tags, right?

But if I don't use it, Visual Studio complains:

Visual Studio stops complaining once I reference browser.d.ts.

Here is my tsconfig:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "files": [

  ],
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

I also tried removing the "files" property from the tsconfig as suggested here, but if I do this, I get compilation error in other typescript files inside the ambient folder of typings.

The code runs and all is fine, but I want to understand if I'm doing something wrong or if it's a Visual Studio problem.


回答1:


For those who might have the same issue in the future:

I fixed the issue. The secret is to exclude the "files" property from tsconfig and in the "excluded" section add the folder "typings".

The tsconfig should now look like that:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "typings",
    "node_modules",
    "wwwroot"
  ]
}



回答2:


In my case I fixed adding "jasmine" into "types" section in tsconfig.json. Like this:

{
    "compilerOptions": {
      "noImplicitAny": false,
      "noEmitOnError": true,
      "removeComments": false,
      "sourceMap": true,
      "target": "es5"
    },
    "exclude": [
      "typings",
      "node_modules",
      "wwwroot"
    ],
    "types": ["jasmine"]
}


来源:https://stackoverflow.com/questions/36080113/typescript-error-cannot-find-name-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!