简体   繁体   中英

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:

/** @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. Is it possible to tell WebStorm how ApplicationSettings type should look like without explicitly specifying all of its properties in JSDoc?

Why do you need @typedef here? the following syntax works:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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