如何让 VScode 识别 d.ts 文件而不直接引用它们 [英] How to get VScode recognize d.ts files without referencing them directly

查看:221
本文介绍了如何让 VScode 识别 d.ts 文件而不直接引用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 create-react-app-ts<创建了一个项目/p>

我还有一组 d.ts 文件,它们是从 JSON 模式生成的接口.他们为远程 API 定义了一些接口.

我希望这些 d.ts 文件在整个项目中全局"可用,而无需通过文件名直接引用它们.有点类似于 Promise 定义如何全局可用.

我尝试修改 tsconfig,将 path/to/**/*.dts 添加到 include,以及在 <代码>文件.我还尝试在 compilerOptopns 下添加到 "typeRoots" 的路径.

这会使项目编译,但 VSCode 没有为这些接口提供 IntelliSense,并在它们下划线为未知.

解决方案

基于 tsconfig 编译器选项,您应该使用选项 typeRoots.本节文档描述了不同之处@typestypeRootstypes 之间.

对于您的具体情况,这个 tsconfig.json 文件应该可以解决问题:

<代码>{编译器选项":{"typeRoots" : ["./type-definitions-folder"]}}

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

<代码>{编译器选项":{"typeRoots" : ["./node_modules/@types", "./type-definitions-folder"]}}

I have a project created with create-react-app-ts

I also have a set of d.ts files that are generated interfaces from JSON-schema. They define some interfaces for a remote API.

I would like these d.ts file to be "globally" available throughout the project, without the need to directly reference them by file name. Somewhat similar to how Promise<T> definition is available globally.

I've tried modifying tsconfig, added path/to/**/*.dts to include, as well as in files. I also tried adding path to "typeRoots" under compilerOptopns.

This makes the project compile, but VSCode provides no IntelliSense to these interfaces, and underlines them as unknown.

解决方案

Based on tsconfig compiler options, you should use option typeRoots. This section of documentation describes the difference between @types, typeRoots and types.

For your specific case this tsconfig.json file should do the trick:

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

If you are also using type definitions from npm you should also add ./node_modules/@types.

{
    "compilerOptions": {
        "typeRoots" : ["./node_modules/@types", "./type-definitions-folder"]
    }
}

这篇关于如何让 VScode 识别 d.ts 文件而不直接引用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆