[英]JSDoc: How do I let IntelliSense/TypeScript know about classes from other files?
I'm working on a relatively large project, and I want to add JSDoc to my classes and files to make it a lot easier to develop.我正在处理一个相对较大的项目,我想将 JSDoc 添加到我的类和文件中,使其更容易开发。 I have a getter function for my "Configuration" class/object that returns its instance of a "SQLRegistry" object, which is set later on down the chain.
我的“配置”类/对象有一个 getter 函数,该函数返回其“SQLRegistry”对象的实例,该对象稍后在链中设置。
//configuration.js
/**
* @returns {SQLRegistry} registry
*/
getRegistry() {
return this._registry;
}
//sqlRegistry.js
const Configuration = require('./configuration');
class SQLRegistry {
//other code
}
Unfortunately, in VS Code with IntelliSense/ESLint, it provides an error saying that it cannot find the class 'SQLRegistry'.不幸的是,在带有 IntelliSense/ESLint 的 VS Code 中,它提供了一个错误,指出它找不到类“SQLRegistry”。 In any other situation, I would just import sqlRegistry.js into configuration.js, but in this situation I cannot (because sqlRegistry depends on configuration.js, as you can see above).
在任何其他情况下,我只会将 sqlRegistry.js 导入 configuration.js,但在这种情况下我不能(因为 sqlRegistry 依赖于 configuration.js,如上所示)。
Is there some sort of JSDoc comment I can put at the top of the file that tells it to read sqlRegistry.js, so that it becomes aware of the SQLRegistry class?是否有某种 JSDoc 注释可以放在文件顶部,告诉它读取 sqlRegistry.js,以便它知道 SQLRegistry 类? For example, something like:
例如,类似于:
/**
* @include {@link ./sqlRegistry.js}
*/
Your hypothetical @include {@link ./sqlRegistry.js}
was very close in principal.您假设的
@include {@link ./sqlRegistry.js}
在原则上非常接近。
The actual way to write this under TypeScript's interpretation of JSDoc syntax is在 TypeScript 对 JSDoc 语法的解释下写这个的实际方法是
/**
* @returns {import('./sqlRegistry.js')} registry
*/
getRegistry() {
return this._registry;
}
Note that it is TypeScript that actually powers the VS Code's primary JavaScript features like type inference, not ESLint请注意,实际上是 TypeScript 为 VS Code 的主要 JavaScript 功能(如类型推断)提供支持,而不是 ESLint
This syntax, import('module-specifier')
in a type position, is known as import types , not to be confused with Type only imports and was introduced in TypeScript 2.9.这种在类型位置
import('module-specifier')
语法被称为import types ,不要与Type only 导入混淆,它是在 TypeScript 2.9 中引入的。
This is not a JSDoc specific feature but rather comes from TypeScript.这不是 JSDoc 特定的功能,而是来自 TypeScript。
The TypeScript language service recognizes the syntax in JSDoc comment locations where a type is expected. TypeScript 语言服务识别 JSDoc 注释位置中需要类型的语法。 Note that the official TypeScript Handbook states that
请注意,官方TypeScript 手册指出
You can also import declarations from other files using import types.
您还可以使用导入类型从其他文件导入声明。 This syntax is TypeScript-specific and differs from the JSDoc standard:
此语法特定于 TypeScript,与 JSDoc 标准不同:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.