如何获取VS Code以了解跨多个文件的JSDOC的@typedef [英] How to get VS Code to understand JSDOC's @typedef across multiple files

查看:150
本文介绍了如何获取VS Code以了解跨多个文件的JSDOC的@typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个文件中指定一种类型,并能够在另一个文件中重用它.我尝试了这是我找到的最接近的问题.

I want to specify a type in one file, and be able to reuse it in another one. I tried modules but it didn't work in VS Code. Is there any other solution? Just wanna have all types for my project to be reusable so I can reference them in different functions across files. This is the closest question I have found.

推荐答案

使用 include 属性在Visual Studio Code 1.33.1中的纯JavaScript项目中

I've had some success with using jsconfig.json and its include property in a plain JavaScript project in Visual Studio Code 1.33.1

{
  "include": [
    "src/**/*.js"
  ]
}

给出以下JavaScript项目:

Given the following JavaScript project:

src/
├── types/
|   ├── person.js
|   ├── question.js
|
├── answer.js
├── jsconfig.json

question.jsperson.js都是类型定义:

person.js

/**
 * @typedef {object} Person
 * @property {string} firstName
 * @property {string} lastName
 */

question.js

/**
 * @typedef {object} Question
 * @property {Person} askedBy
 * @property {string} text
 */

answer.js是一个接受问题并返回答案的函数:

And answer.js is a function that accepts a question and return an answer:

/**
 * Takes a question and return an answer
 * @param {Question} question 
 */
function answer(question) {
  return 42;
}

正如您在第一个截屏中看到的那样,当将鼠标悬停在Question类型符号上时,我确实获得了IntelliSense支持:

As you can see in the first screencast I do get IntelliSense support when hovering over the Question type notation:

最重要的是,IntelliSense现在还能够根据我的类型定义提供代码完成:

On top of that IntelliSense is also now able to offer code completion based on my types definitions:

这篇关于如何获取VS Code以了解跨多个文件的JSDOC的@typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆