代码之家  ›  专栏  ›  技术社区  ›  dark_ruby

如何在不直接引用的情况下让VScode识别d.ts文件

  •  2
  • dark_ruby  · 技术社区  · 8 年前

    我有一个用 create-react-app-ts

    d.ts 从JSON架构生成接口的文件。它们为远程API定义了一些接口。

    d、 ts公司 文件在整个项目中“全局”可用,无需通过文件名直接引用它们。有点像 Promise<T> 定义在全球范围内可用。

    我试过修改 tsconfig path/to/**/*.dts include ,以及 files . 我还尝试添加路径到 "typeRoots" compilerOptopns .

    这使得项目可以编译,但是VSCode没有为这些接口提供智能感知,并将它们强调为未知。

    1 回复  |  直到 8 年前
        1
  •  5
  •   dotnetom    8 年前

    基于 tsconfig compiler options typeRoots . This section 描述了 @types , 类型根 和 types

    对于你的具体情况 tsconfig.json 文件应该做到:

    {
        "compilerOptions": {
            "typeRoots" : ["./type-definitions-folder"]
        }
    }
    

    如果您还使用npm的类型定义,那么还应该添加 ./node_modules/@types .

    {
        "compilerOptions": {
            "typeRoots" : ["./node_modules/@types", "./type-definitions-folder"]
        }
    }
    
        2
  •  1
  •   Cezar Augusto    6 年前

    如果TypeScript编译时没有错误,但是VSCode仍然无法识别全局类型并添加适当的intellisense,那么 很可能您的项目包含太多文件,而您对希望TypeScript检查的文件不够具体。

    使用 typeRoots 如果没有实现,请考虑指定intellisense在中工作的所有路径 include exclude 字段。 VSCode应该跟在后面,并将intellisense添加到丢失的文件中。

    {
      "compilerOptions": {
        "typeRoots": [
          "path/to/**/*.d.ts"
        ],
        "include": [
          // Add all paths you want intellisense to work
          "path/to/where/you/expect/intellisense/to/work",
          "path/to/another/file/or/folder"
        ]
      }
    }
    
    推荐文章