Skip to content

[Salsa JSDoc] Using express types with require #21551

@ekulabuhov

Description

@ekulabuhov

I'm trying to add types from 'express' module in nodejs code. I'm not using any compilers. Below are 3 different ways that I've tried with various levels of success. Is there a right way to do it?

TypeScript Version: VSCode 1.19.3

Search Terms:
Salsa, JSDoc, Express
Code

  1. Require doesn't work as expected:
var express = require('express')

/**
 * 
 * @param {express.Request} req 
 * @param {express.Response} res 
 */
function routeHandler(req, res) {
    // req: any
    // res: any
}
  1. Import is working as expected but not supported by nodejs natively:
import express from "express";

/**
 * 
 * @param {express.Request} req 
 * @param {express.Response} res 
 */
function routeHandler(req, res) {
    // req: express.Request
    // res: express.Response    
}
  1. This is the only way I found to make it work. But making a call to express() is not without side effects. I would like to avoid that.
var express = require('express');
var { request, response } = express();

/**
 * 
 * @param {request} req 
 * @param {response} res 
 */
function routeHandler(req, res) {
    // req: Request
    // res: Response
}

Expected behavior:
Expecting require to work similarly to import.
Actual behavior:
Require is not importing the right types.
Related Issues:
#14377

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions