简体   繁体   English

JSDoc:如何让 IntelliSense/TypeScript 了解其他文件中的类?

[英]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,如上所示)。

TypeScript jsdoc 检查器给我一个错误,说它找不到名称“SQLRegistry”

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.

相关问题 如何让一个屏幕知道其他屏幕的状态 - How do I let a screen know other screen's state 如何使 VSCode 中的 IntelliSense 显示其他文件的完成? - How do I make IntelliSense in VSCode show completion from other files? 如何让DOM知道动态创建的新元素? - How do I let the DOM know about new elements created on the fly? 如何在 TypeScript 中使用 JSDoc 记录“提供者模式”功能? - How do I document "provider-pattern" functions with JSDoc in TypeScript? 我如何让打字稿停止抱怨它不知道的功能? - How do I get typescript to stop complaining about functions it doesn't know about? JSDoc3如何让它知道变量是对Class原型的引用 - JSDoc3 how to let it know a variable is a reference to a Class prototype IntelliSense/JSDoc @param = @return,也就是我如何记录包装函数? - IntelliSense/JSDoc @param = @return, a.k.a. how do I document wrapper functions? 我如何使用 jsdoc 注释注释 React.forwardRef 以便智能感知可以显示在底层组件中? - How do i annotate React.forwardRef with jsdoc comments so that intellisense can show up for the underlying component? Javascript inheritance 和 JSDoc - 让基础 class 知道派生 ZA2F2ED4F8EBC2CBB14C21A29DC40 中定义的类型 - Javascript inheritance and JSDoc - let base class know about type defined in derived class 如何让 Nuxt 页面知道错误代码状态? - How do I let a Nuxt page know the error code status?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM