Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get VS Code to understand JSDOC's @typedef across multiple files

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.

like image 470
zavr Avatar asked Aug 23 '17 10:08

zavr


People also ask

How do you edit multiple files VS Code?

“vscode edit line in multiple files at once” Code AnswerWindows: Ctrl + Alt + Arrow Keys. Linux: Shift + Alt + Arrow Keys. Mac: Opt + Cmd + Arrow Keys.

How do I open multiple files in VS Code editor?

Launch VS Code and press the “Ctrl” and “P” keys simultaneously to search for a file to open in the current project. Type in the file name. To open the new file in a temporary tab, click on it once. To open the new file in a separate window that you can choose to close manually, double-click it.

How do you link a file with a VS Code?

As one option, we can right-click on the source file and choose "Copy" and then right-click on the destination project and choose "Paste". The keyboard shortcuts Ctrl+C and Ctrl+V also work as long as you have the correct items selected in the Solution Explorer.

How do I open all files in VS Code?

VS Code provides two powerful commands to navigate in and across files with easy-to-use key bindings. Hold Ctrl and press Tab to view a list of all files open in an editor group. To open one of these files, use Tab again to pick the file you want to navigate to, then release Ctrl to open it.


2 Answers

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"   ] } 

Given the following JavaScript project:

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

Where both question.js and person.js are type definitions:

person.js

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

question.js

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

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; } 

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

enter image description here

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

enter image description here

like image 143
customcommander Avatar answered Sep 23 '22 10:09

customcommander


Since TypeScript 2.9 which is embedded in the newer VS Codes, it is possible by using the import syntax in JSDoc, like so

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

Also VS Code should be picking up all types defined across the workspace.

like image 41
zavr Avatar answered Sep 23 '22 10:09

zavr