【问题标题】:How to pull in the typescript hints for a function's argument documentation如何为函数的参数文档提取打字稿提示
【发布时间】:2018-08-03 01:26:12
【问题描述】:

如果我创建了一些 Express.js 中间件,如何让我的编辑器 (VSCode) 提供打字稿提示,就好像我直接引用了库一样?

例如,我可以在@param 部分中输入什么来获取 Express.js 提示?

file.js

const express = require('express');
const app = express();

/**
 * 
 * @param {Express.Request} req 
 * @param {Express.Response} res 
 */
function myMiddleware(req, res, next) {
    if (true) {
        res.json(true); // ** when I hover over `json` no hints are shown **
    } else {
        next();
    }
}

app.use(myMiddleware);

例如,我想将鼠标悬停在 res 上,看看它包含哪些方法/属性。

【问题讨论】:

    标签: node.js typescript express visual-studio-code


    【解决方案1】:

    函数的参数文档

    您的问题实际上是“如何让 TypeScript 使用 JSDoc type 注释”,例如您希望选择的类型:

    /**
     *  @param {Express.Request} req
     *  @param {Express.Response} res
     */
    function myMiddleware(req, res) { ... }
    

    解决方案

    使用.js 文件扩展名(jsdoc 中的类型注释不用于ts 文件)并在您的tsconfig 中使用allowJs: true

    【讨论】:

      【解决方案2】:
      import {Request, Response} from 'express';
      
      /**
       * My middleware
       * @param {Request} req Express Request...
       * @param {Response} res Express Response...
       */
      function middleware(req : Request, res : Response){
          res.send('Hello!'); //You will get hints when using the parameters
      }
      
      middleware(req /* You will get hints when passing the parameters */, res); 
      

      【讨论】:

        猜你喜欢
        • 2019-03-25
        • 2021-09-06
        • 1970-01-01
        • 2020-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        相关资源
        最近更新 更多