【问题标题】:typescript, jsconfig.json, bower.json and vs-code working togethertypescript、jsconfig.json、bower.json 和 vs-code 一起工作
【发布时间】:2026-02-08 20:25:02
【问题描述】:

我最近在使用带有 intelisense 的 Visual Studio 代码时遇到了这段文档:

“您还可以通过使用类型定义 .d.ts 文件来获取库的 IntelliSense。DefinitelyTyped 是所有主要 JavaScript 库和环境的类型文件存储库。使用 Typings 可以轻松管理类型, TypeScript 定义管理器。 例如 typings install --ambient node 安装内置 Node.js 模块的所有类型。 如果您的项目有 jsconfig.json 文件,请确保类型包含在由 jsconfig.json 文件位置定义的项目上下文中。"

(解释于https://code.visualstudio.com/Docs/languages/javascript)。

我的问题是我无法让它工作,我不确定我是否完全理解了以粗体标记的句子。粗体部分是否告诉我在与 jsconfig.json 相同的文件夹中创建名为 typings 或 typings.json 的文件夹,或者两者都需要。它是否告诉我以某种方式修改 jsconfig.json。

那么我如何让 intelisense 像开头所述那样使用我的凉亭包?

jsconfig.json

{
// See http://go.microsoft.com/fwlink/?LinkId=759670
// for the documentation about the jsconfig.json format
"compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "allowSyntheticDefaultImports": true
},
"exclude": [
    "node_modules",
    "bower_components",
    "jspm_packages",
    "tmp",
    "temp"
],
"typings": "typings/"

}

我使用 node.js npm 来获取全局工具,我目前已将以下内容安装为全局:

  • bower@1.7.9
  • esling@2.9.0
  • generator-aspnet@0.0.93
  • less@2.6.1
  • live-server@0.9.2
  • typings@0.8.1
  • yo@1.7.0

我用 bower 做客户端包

我目前的设置是用于开发 javascript 组件,而不是用于网站发布。 folder and file structure

【问题讨论】:

    标签: javascript json typescript bower visual-studio-code


    【解决方案1】:

    所以,粗体字的意思是,如果你有一个纯 JavaScript 项目,并且希望在使用 Visual Studio Code 时获得 IntelliSense 的好处,你需要在你的根目录下添加一个 jsconfig.json 文件项目。

    这个文件可以是空的,它可以工作,但是如果你查看文档,你会发现有一些选项可以改变你的项目。如果你想要的只是 IntelliSense,那么一个空的 jsconfig.json 文件就可以完成这项工作。

    接下来,您需要的是类型定义。您可以在 typings 工具的帮助下添加这些内容。

    像这样全局安装它:

    npm i -g typings
    

    有一些文档在:https://github.com/typings/typings/

    但是假设您在项目中使用 jquery。安装类型后,您可以运行命令:

    typings i jquery --ambient --save
    

    这将为 jquery 安装类型定义,并且因为您有一个 jsconfig.json 文件,在您的 javascript 文件中,您在使用 jQuery 时会有 IntelliSense。

    【讨论】:

    • 输入 jquery --ambient --save。找不到 jquery,因为它不属于 github 上的 registry/npm 文件夹。但是,在进行打字搜索时 -h。它告诉我我可以将 --source 指定为 npm 或 bower,但 bower 似乎存在?
    • 这不是问题,你不会通过打字来安装jquery。您将安装类型。来源是它可以找到类型的地方。
    • 我不想只安装 jquery 的类型,但是如果类型 i jquery --ambiant --save 说它找不到 jquery,我很确定它不会为我输入 jquery 跨度>
    • 您不会通过键入该命令来安装 jquery。只会安装类型。并且它的 --ambient 带有 E 而不是 A。我刚刚复制/粘贴了命令并尝试了它并且它有效