Search code examples
javascriptdocumentationjsdoceslint-plugin-jsdoc

JSDoc : @typedef {Object} .. How to document a method that is inside the object at @typedef


This is what I did so far. I want to document the method lufthansa.book How should I approach It ? Should I document It inside the Object like below? Or In the @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");

The example below didn't work. If I use this vscode initiates type check message

Parameter 'flightNum' implicitly has an 'any' type, but a better type may be inferred from usage. this is what I receive for both param if I use the approach below


/**
 * 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");


Solution

  • There are several solutions. If you want to keep type definition separately from the code, you can write something like this:

    /**
     * @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}
          `);
      },
    };
    

    or you can go backward and infer type:

    /**
     * @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");