[英]Why's that vscode cant autocomplete exported objects on its own, from one file in another, in nodejs? Is it a performance feature?
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 或其相应属性的自动完成。
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
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
是相应的续集方法和属性也应该自动完成。
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
。
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
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.