Functions: optional parameters, overloads


#1

Hello,

we use TernJS in combination with CodeMirror to provide an editor to our customers that works well with our library. So far, we are quite happy with the features TernJS provides.

Our library uses some features, we currently could not find a satisfying way to map to the TernJS definition files. Those are:

  • Functions with optional/default parameters
  • Function overloads

The best solution we came up with looks like this:

{
  "!define": {
    "MyFuncNamedArgs": {
      "a": "number",
      "b": "string"
    }
  },
  "myFunc": "fn(a: number, b: string)|fn(obj: MyFuncNamedArgs)"
}

representing the function

/**
 * @function myFunc
 * @param {number} a
 * @param {string=""} b
 */

/**
 * @function myFunc
 * @param {Object} obj
 * @param {number} obj.a
 * @param {string=""} obj.b
 */

This is able to somehow mirror the overload of the function, as I could see debugging the CodeMirror Tern plugin: The returned type of myFunc is fn(a: number, b: string)|fn(obj: MyFuncNamedArgs). However, the plugin still only shows the first overload as argument hint, the second one is simply omitted.

We could not find a method to mirror the optional/default arguments.

What would be optimal way to represent those features in TernJS? Are we missing something?

Best regards!