带有导入语句的 TypeScript 智能感知 - VS Code [英] TypeScript intellisense with import statement - VS Code

查看:31
本文介绍了带有导入语句的 TypeScript 智能感知 - VS Code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 VSCode 智能感知能够理解我的 TS import 语句的语义.如果是这样,那么 VSCode 可以尝试将这些语句解析为文件并从这些文件中提取类型而无需任何注释.有时它可以,如果它可以找到导出我正在导入的内容的打字稿文件.但是,有时它不能这样做,特别是当我导入一个带有环境声明类型的库时.但是我想导入的几乎所有东西都带有环境声明,所以这很重要.

I would like VSCode intellisense to understand the semantics of my TS import statements. If it did, then VSCode could try to resolve those statements to files and pull types from those files without any annotations. Sometimes it can, if it can find the typescript file that exports what I'm importing. However, there are times when it can't do that, specifically, when I'm importing a library that has been typed with an ambient declaration. But almost everything I want to import is typed with an ambient declaration, so this is a big deal.

我知道这很复杂,因为 import 语句根据模块标志编译成不同的东西,并且生成的 import 语句可以有一个额外的运行时路由层,这会混淆一个简单的文件基于搜索策略.

I understand that this is complicated by the fact that import statements compile to different things depending on the module flag, and that generated import statements can have an additional layer of runtime routing that confuses a simple file-based search strategy.

在一个完美的世界里,既然我们在做梦,我希望能够引用一个 requirejs config 对象,定义为独立的 json 文件或在 tsconfig.json 中逐字定义,并让 VS Code 使用 requirejs 路由语义 将导入的模块 ID 解析为磁盘上的文件(否则在编译时当我的导入语句无法解析为模块时警告我运行时).

In a perfect world, since we're dreaming, I would like to be able to reference a requirejs config object, defined as a standalone json file or literally defined in tsconfig.json, and have VS Code use requirejs routing semantics to resolve the imported moduleid to a file on disk (or else warning me at compile time when my import statement won't resolve to a module at runtime).

但是,即使能够仅让智能感知搜索列表中的所有路径以启发式地找到我正在导入的模块也会很好,这与模块加载器无关.像 requirejs 这样的路由层被强加在上面,理论上可以解析到与 TS 找到的模块不同的模块;在大多数情况下它会起作用,但可以通过注释处理奇怪的问题.

However, even being able to just have intellisense search all of the paths in a list to heuristically find the module that I'm importing would be nice, and that would be module-loader agnostic. With a routing layer like requirejs imposed over top, it would be possible in theory to resolve to a different module than the one that TS finds; in most cases it would work, but the oddballs could be handled with an annotation.

我认为目前在任何编辑器中都无法实现这些功能,包括 VSCode.很可能他们将来会在那里,但就目前而言,我很好奇其他人是如何获得智能感知的.我愿意竭尽全力避免 注释,因为它们是代码重复.

I don't think either of these things is currently possible in any editor, including VSCode. Quite possibly they'll be there in the future, but for now, I'm curious how other people are getting intellisense. I'm willing to go to extreme lengths to avoid the <reference path=.../> annotations, since they are code duplication.

我的一些库是用绝对类型包类型化的,但其中一些不是,例如,其中一些是已浏览器化的内部模块.所以 tsd 不是一个完整的解决方案.

Some of my libraries are typed with DefinitelyTyped packages, but some of them are not, e.g., some of them are internal modules that have been browserified. So tsd is not a full solution.

解决方案"的一个级别是简单地使每个文件中的所有项目类型都可用,例如通过合并整个项目中的每个类型声明,包括来自 .d.ts 文件的环境模块,然后引用该聚合,只要两个环境模块没有命名空间冲突.此解决方案在发生命名空间冲突之前一直有效,然后它会永久停止工作.所以我不太喜欢这个主意.

One level of "solution" is to simply make all the types of the project available in every file, e.g. by coalescing every type declaration from the entire project, including ambient modules from .d.ts files, and then referencing that agglomeration, so long as two ambient modules don't have a namespace collision. This solution works until there is a namespace collision, and then it permanently stops working. So I don't really like this idea.

还有什么更好的吗?有没有人让 tsserver 工作?我几乎找不到关于它的文档——我只知道它存在是因为我注意到它是在我安装 node 的 tsc 实现时安装的.

Is there anything else better? Has anyone gotten tsserver to work? I can find scant little documentation about it - I only know it exists because I noticed that it was installed when I installed node's tsc implementation.

推荐答案

TypeScript 团队随后解决了我的所有问题.他们为 tsconfig.json 提供了扩展,允许我们声明模块解析并模仿所有主要模块系统(requirejs、webpack、browserify)的解析策略.

All of my concerns were subsequently addressed by the TypeScript team. They have provided extensions to tsconfig.json which allow us to declare module resolution and mimic the resolution strategy of all the major module systems (requirejs, webpack, browserify).

一切都在这里https://www.typescriptlang.org/docs/手册/模块分辨率.html.

这篇关于带有导入语句的 TypeScript 智能感知 - VS Code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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