How to tell JSDoc that a method returns an object containing a class type?

2.8k Views Asked by At

I have a Node.js function to create an Express server.

I've added //@ts-check to the file so that VSCode lints and type-checks everything.

What is the correct way to document this createServer function?

I want to show that it returns a Promise which resolves an object containing an Express instance (app), and the Number (port)

If this is an anti-pattern, that would be nice to know as well.

//@ts-check
const express = require('express');

/**
 * @param {Number} port
 * @returns {Promise}
 */
function createServer(port) {
    return new Promise((resolve, reject) => {
        const app = express();
        server = app.listen(port, () => {
            resolve({app, port});
        });
    });
}
1

There are 1 best solutions below

1
Matt McCutchen On BEST ANSWER

This compiles for me:

//@ts-check
const express = require('express');

/**
 * @param {Number} port
 * @returns {Promise<{app: import("express").Express, port: number}>}
 */
function createServer(port) {
    return new Promise((resolve, reject) => {
        const app = express();
        const server = app.listen(port, () => {
            resolve({app, port});
        });
    });
}

See the documentation page for more information.