简体   繁体   English

为什么 vscode 不能自动完成导出的对象,从一个文件到另一个文件,在 nodejs 中? 它是性能特征吗?

[英]Why's that vscode cant autocomplete exported objects on its own, from one file in another, in nodejs? Is it a performance feature?

Problem问题

In my usecase, i maintain my database config/connections using sequelize in a separate file models/index.js .在我的用例中,我使用sequelize在单独的文件models/index.js中维护我的数据库配置/连接。 I export the database connection object(called db ) so that other modules can connect to the database.export数据库连接对象(称为db ),以便其他模块可以连接到数据库。 But when I require the object in some other file, no auto-completions are shown for that object nor its corresponding properties.但是当我在其他文件中require object 时,不会显示该 object 或其相应属性的自动完成。

Context语境

This is how my part of my models/index.js file looks like, which i have done like the official sequelize example :-这就是我的models/index.js /index.js 文件的一部分的样子,就像官方的续集示例一样:-

const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(config);
let db = {};
// for each of the files `file` in a dir...dynamically do(refer above linked example for details):-
let model = require(path.join(__dirname, file))(sequelize, Sequelize);
db[model.name] = model; // type Sequelize.Model
// dynamic config over

// statically add
db.sequelize = sequelize; // type Sequelize
module.exports = db

This is how i have imported in another module:-这就是我在另一个模块中导入的方式:-

const db = require("./models/index.js");
db.seq -------------------------------> where i try for auto-completion

Expectation期待

When i require my database connection object db in another file, it should provide auto-completion for the properties i statically assigned, like db.sequelize should be a completion and further db.sequelize.xxx , where xxx is the corresponding sequelize methods and properties should also be auto-completed.当我在另一个文件中require我的数据库连接 object db时,它应该为我静态分配的属性提供自动完成,比如db.sequelize应该是一个完成,然后是db.sequelize.xxx ,其中xxx是相应的续集方法和属性也应该自动完成。

What's the behavior now?现在是什么行为?

Typing db.键入db. doesn't show any valid auto-completions pertaining to the object db .没有显示任何与 object db相关的有效自动完成。 Also db.sequelize has a type any for the sequelize part(so no further auto-completions), whereas I expected it to be Sequelize . db.sequelize也有一个用于sequelize部分的类型any (所以没有进一步的自动完成),而我希望它是Sequelize

What I have done?我做了什么?

  • Explicitly added jsdoc , thinking that's the only way to make the auto-completions work, but to no use:-明确添加jsdoc ,认为这是使自动完成工作的唯一方法,但没有用:-
/**
* @typedef {import('sequelize').Sequelize} Sequelize
*/

const { Sequelize } = require('sequelize');
/**
 * @type {Sequelize}
 */
const sequelize = new Sequelize(config);
/**
 * key : string -> Name of the Sequelize.Model | 'sequelize'
 * value : Either one of the Models or Sequelize object
 * @type {Object.<string,(Sequelize|Sequelize.Model)>}
 * @property {Sequelize} sequelize
 * @example
 * db = { User: db.User , seqeuelize : sequelize}
 */
let db = {};
// rest of the dynamic processing and adding Sequelize.Model to db
db.sequelize = sequelize;
module.exports = db
  • Added a jsconfig.json with following content, but that was also futile:-添加了具有以下内容的jsconfig.json ,但这也是徒劳的:-
{
  // NOTE THAT, this file resides within my src/server/ directory where all the js files exists
  // the models/index.js resides within src/server
  // I maintain another jsconfig for src/client
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "experimentalDecorators":true,
    "baseUrl": ".",
    "paths": {
      "*": ["*"],
    },
  },
  "include": ["./**/*"],
}

If you change your object let db = {};如果你改变你的 object let db = {}; to let db = {sequelize: null}; let db = {sequelize: null}; , you will get intellisense to work as intended. ,您将获得智能感知按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从导出的模块返回变量,并在另一个文件(NodeJS)中使用它 - Return variable from exported module and use it in another file (NodeJS) 为什么我不能在Node.js中将另一个文件中的导出变量设置为另一个文件? - Why can't I set an exported variable in one file from another in Node.js? 如何使vscode显示来自另一个.js文件的导出函数的类型信息? - How to make vscode show type info for exported function from another .js file? 我自己的javascript文件需要智能感知或自动完成功能 - Need intellisense or autocomplete feature for my own javascript file 不能从导入的文件中使用功能到另一个功能? - cant use function into another one from imported file? 如何从一个文件导出变量,并在另一个文件上require()导出的变量? - How can I export a variable from one file and require() the exported variable on another file? 如何从Node.js项目中的另一个文件访问文件 - How to access a file from one another file in nodejs project 将SELECT sql变量数组从一个文件发送到Node.js中的另一个文件 - Sending a SELECT sql variable array from one file to another in Nodejs 将变量从一个js传递到node.js中的另一个js文件 - Passing variable from one js to another js file in nodejs 无法从 nodejs 中的 typescript 文件中要求默认导出函数 - not able to require default exported function from typescript file in nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM