在jsdoc中引用一个typescript类

问题描述 投票:0回答:1

我一直在使用 jsdoc 记录我的 JS,现在正在转换为 d.ts 文件,以便将它们作为类型从我的包中导出。我想让事情尽可能保持干燥。

declare class CallHistory {
    constructor(globalConfig: FetchMockConfig, router: Router);
    ...
}
export default CallHistory
/**
 * @import CallHistory from '../types/CallHistory';
 */

class CallHistory {
    constructor(globalConfig, router) {
        /** @type {CallLog[]} */
        this.callLogs = [];
        this.config = globalConfig;
        this.router = router;
    }
}

我收到警告,指出

globalConfig
router
“隐式具有“任何”类型。”

我尝试添加

/** @type {CallHistory} */
/** @class {CallHistory} */
/** @class CallHistory */
/** @class */
/** @type {typeof CallHistory} */
,以便获取使用 CallHistory.d.ts 文件中的类定义中的类型的方法,但没有任何作用。

如何让类型检查器了解我的 js 类是 ts 类的实现?

javascript typescript class jsdoc
1个回答
0
投票

.d.ts
文件旨在将 JavaScript 库导入 TypeScript 项目时使用;即,它们为 JavaScript 库的用户提供类型注释,而不是实现本身。

据我所知,最接近的方法是构建您的项目,使每个模块的

.d.ts

 文件与您的 
.js
 文件位于同一目录中,如下所示:

  • /
    
    
    • CallHistory.d.ts
      
      
    • CallHistory.js
      
      
    • index.js
      
      
这样,虽然

CallHistory.js

 本身没有类型注释,但项目中使用它的每个其他文件都有类型注释。例如,如果 
index.js
 开头为:

import CallHistory from './CallHistory';
然后 

CallHistory

 中导入的 
index.js
 将被正确键入注释:
Annotated constructor

同时具有类型注释并让它们在实现文件中工作的唯一其他选择(据我所知)是专门使用 JSDoc 并在构建步骤期间或推送到远程时生成

.d.ts

 文件回购。

© www.soinside.com 2019 - 2024. All rights reserved.