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
withERROR - 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
}
}