JSDoc : @typedef {Object} .. 如何记录 @typedef 对象内部的方法

Toh*_*lam 4 javascript documentation jsdoc eslint-plugin-jsdoc

这就是我到目前为止所做的。我想记录该方法lufthansa.book我应该如何处理它?我应该将其记录在对象内,如下所示?或者在@typedef {Object} Airline


/**
 * This is a predefinition of the method that is inside the Object
 * It will be used as the type at @property {Type} for the method
 * BookMethod will be used the type of lufthansa.book
 * @typedef {Function} BookMethod 
 * @returns {void}
 */

/**
 * @typedef {Object} Airline 
 * @property {String} airline
 * @property {String} iataCode
 * @property {Array} bookings The array of bookings
 * @property {BookMethod} book
 */

/**
 * @name lufthansa
 * @type {Airline}
 */
const lufthansa = {
    airline: "Lufthansa",
    iataCode: "LH",
    bookings: [],
    /**
     * @type {BookMethod}
     * @param {Number} flightNum
     * @param {String} name
     */
    book(flightNum, name) {
        console.log(`
        ${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
        `);
    },
};

lufthansa.book(2321, "Jean Steel");

Run Code Online (Sandbox Code Playgroud)

下面的例子没有成功。如果我使用此 vscode 启动类型检查消息

Parameter 'flightNum' implicitly has an 'any' type, but a better type may be inferred from usage.如果我使用下面的方法,这就是我收到的两个参数


/**
 * This is a predefinition of the method that is inside the Object
 * It will be used as the type at @property {Type} for the method
 * @typedef {Function} BookMethod 
 * @param {Number} flightNum
 * @param {String} name
 * @returns {void}
 */

/**
 * This predefinition for the Object 
 * @typedef {Object} Airline 
 * @property {String} airline
 * @property {String} iataCode
 * @property {Array} bookings The array of bookings
 * @property {BookMethod} book
 */

/**
 * @name lufthansa
 * @type {Airline}
 */
const lufthansa = {
    airline: "Lufthansa",
    iataCode: "LH",
    bookings: [],
    book(flightNum, name) {
        console.log(`
        ${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
        `);
    },
};

lufthansa.book(2321, "Jean Steel");

Run Code Online (Sandbox Code Playgroud)

mak*_*imr 5

有几种解决方案。如果您想将类型定义与代码分开,您可以编写如下内容:

/**
 * @typedef {Object} Airline 
 * @property {String} airline
 * @property {String} iataCode
 * @property {Array} bookings The array of bookings
 * @property {(flightNum: number, name: string) => void} book Book a seat on a flight
 */

/**
 * @name lufthansa
 * @type {Airline}
 */
const lufthansa = {
  airline: "Lufthansa",
  iataCode: "LH",
  bookings: [],
  book(flightNum, name) {
    console.log(`
      ${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
      `);
  },
};
Run Code Online (Sandbox Code Playgroud)

或者你可以向后推断类型:

/**
 * @name lufthansa
 */
const lufthansa = {
  airline: "Lufthansa",
  iataCode: "LH",
  /**
   * @description The array of bookings
   * @type {Array}
   */
  bookings: [],
  /**
   * @description Book a seat on a flight
   * @param {number} flightNum 
   * @param {string} name 
   */
  book(flightNum, name) {
    console.log(`
      ${name} booked a seat on ${this.airline} flight ${this.iataCode}${flightNum}
      `);
  },
};

/**
 * @typedef {typeof lufthansa} Airline
 */

lufthansa.book(1, "Jean Steel");
Run Code Online (Sandbox Code Playgroud)