How to get VS code to understand JSDOC @typedef for multiple files

I want to specify the type in one file and be able to reuse it in another. I tried the modules , but it did not work in VS Code. Is there any other solution? I just want all types for my project to be reused, so I can refer to them in different functions between files. This is the closest question I have found.

+12
source share
2 answers

Starting with TypeScript 2.9, which is built into the new VS codes, this is possible using syntax importin JSDoc, for example,

/**
 * @typedef {import("koa").Context} Context
 *
 * @typedef {Object} BodyparserOptions
 * @prop {(ctx: Context) => boolean} [detectJSON] Custom json request detect function. Default 'null'.
 */

VS Code , .

+2

jsconfig.json include JavaScript Visual Studio Code 1.33.1

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

JavaScript:

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

question.js person.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 - , :

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

, IntelliSense Question:

enter image description here

, IntelliSense :

enter image description here

0

All Articles