簡體   English   中英

JS Doc:如何將 javascript es6 class 返回的 object 定義為自定義類型?

[英]JS Doc: How to define the object returned by a javascript es6 class as a custom type?

我有一些以下代碼:

export class Logger {
   constructor(label = null, options = {}) {
      if (!winston.loggers.has(label)) {
         winston.loggers.add(
            label,
            { /* ...default options */ }
         );
      }

      return winston.loggers.get(label);
   }

   // some other function...
}

在這里,我的 class 返回一個自定義 object,它是一個 Winston 記錄器 object。 But when I import this class in my other files and create a new object with it ( const logger = new Logger() ), the auto-suggestion show only the function in the class ie // some other function . 現在,由於我的 class 返回一個 Winston 記錄器實例,我希望能夠用 JS 文檔記錄它,以便我得到我的自動建議。

有沒有辦法可以使用 JS 文檔來實現這一點,以定義已定義的 class 返回的自定義類型 object?

你在使用 typescript 編譯器嗎? 如果那么您可以定義一個logger.types.ts類型文件並將其導入 jsdoc 返回注釋。

/**
 * Logger Component of the form
 * @returns {import("./logger.types.ts").LoggetProps}
 */

export interface LoggetProps extends winston {
     //example props
     isError: boolean


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM