简体   繁体   中英

Closure Compiler Externs for Function with static fields

What is the correct type for a constructor function that also has properties on the function object with Google's closure compiler?

Here's a runnable first attempt on the Closure compiler debugger .

Application Code

const Mocha = /** @type {!MochaJS} */ (require('mocha'));

const mochaInstance = new Mocha();
const Suite = Mocha.Suite;

Closure Externs

/** @constructor */
const MochaJS = function() {};

/** @type {!MochaJS.Suite} */
MochaJS.prototype.Suite;

/** @record */
MochaJS.Suite = function() {};

The difficulty comes because Closure-compiler doesn't handle external module definitions well. Also, dont' confuse the constructor/namespace with an instance. They are different.

Application

// A constructor type for Mocha
const Mocha = /** @type {!function(new:MochaJS)} */ (require('mocha'));
const mochaInstance = new Mocha();

const Suite = /** @type {!MochaJSSuite} */ (Mocha.Suite);

Externs

/** @constructor */
const MochaJS = function() {};

/** @function */
MochaJSSuite = function() {};

This is just rough guesses for the types - I'm not familiar enough with Mocha to write the externs without going to hunt down the documentation reference. Hopefully it will point you in the right direction though.

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