3

In ES6/ES2015, using interfaces and implementations (@interface and @implements),

Is specifying that a parameter is optional part of the interface or the implementation?

If part of the interface, what about specifying the default value for the optional parameter?

  • In Scenario A, transpiling to ES5 with JSCompiler will object to the interface for otherMethod with ERROR - interface member functions must have an empty body (i.e. that the default value is syntactic sugar for preface code that sets a parameter's value when the value is undefined)

Are the answers the same whether ES5 or ES6?

Scenario A

/**
 * @interface
 */
class SomeInterface {
  /**
   * @param (boolean=) someArgument
   */
  someMethod(someArgument) {}

  /**
   * @param (boolean=) someArgument
   */
  otherMethod(someArgument = false) {}
}


/**
 * @implements {SomeInterface}
 */
class SomeImplementation {

  /** @override */
  someMethod(someArgument = false) {
    // do something
  }

  /** @override */
  otherMethod(someArgument = false) {
    // do something
  }
}

Scenario B

/**
 * @interface
 */
class SomeInterface {
  /**
   * @param (boolean) someArgument
   */
  someMethod(someArgument) {}
}

/**
 * @implements {SomeInterface}
 */
class SomeImplementation {

  /**
   * @param (boolean=) someArgument
   * @override
   */
  someMethod(someArgument = false) {
    // do something
  }
}
jmsgomes
  • 51
  • 5
  • *"Is specifying that a parameter is optional part of the interface or the implementation?"* Certainly the interface. The caller, who implements against the interface has to know whether it does or does not have to pass the argument. – Felix Kling Jun 20 '17 at 23:05

0 Answers0