简体   繁体   English

使用变量定义JSDoc类型[WebStorm友好]

[英]Define JSDoc type using a variable [WebStorm-friendly]

In the following code I would like to define a shape of ApplicationSettings type using testSettings variable, so that inside useSettings function IDE would know which properties are accessible on the settings object: 在下面的代码中,我想使用testSettings变量定义ApplicationSettings类型的形状,以便在useSettings函数内部可以知道在settings对象上可以访问哪些属性:

/** @typedef ApplicationSettings */
var testSettings = {
    apiService: {},
    configuration: {}
};

/**
 * @param {ApplicationSettings} settings
 */
function useSettings(settings) {
    console.log(settings.apiService); // apiService is not recognized here
    console.log(settings.configuration); // same for configuration
}

Unfortunately, seems that @typedef is not a valid annotation in that case. 不幸的是,在这种情况下, @typedef似乎不是有效的注释。 Is it possible to tell WebStorm how ApplicationSettings type should look like without explicitly specifying all of its properties in JSDoc? 是否可以告诉WebStorm,而无需在JSDoc中显式指定其所有属性, ApplicationSettings类型应该是什么样?

Why do you need @typedef here? 为什么在这里需要@typedef? the following syntax works: 以下语法有效:

var testSettings = {
    apiService: {},
    configuration: {}
};

/**
 * @param {testSettings} settings
 */
function useSettings(settings) {
    console.log(settings.apiService); 
    console.log(settings.configuration);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM