Repository navigation
Closed
Milestone
Description
TypeScript Version:
1.8.7
Code
Transpiling _apply.js of lodash-es@4.6.1 with the command
tsc --allowJs _apply.js --out apply.js -m amd
breaks the code. Apparently, the (arguably invalid) @param {...*} args JSDoc comment causes the TypeScript compiler to generate code as if the parameter declaration was apply(func, thisArg, ...args), although it is not.
/**
* A faster alternative to `Function#apply`, this function invokes `func`
* with the `this` binding of `thisArg` and the arguments of `args`.
*
* @private
* @param {Function} func The function to invoke.
* @param {*} thisArg The `this` binding of `func`.
* @param {...*} args The arguments to invoke `func` with.
* @returns {*} Returns the result of `func`.
*/
function apply(func, thisArg, args) {
var length = args.length;
switch (length) {
case 0: return func.call(thisArg);
case 1: return func.call(thisArg, args[0]);
case 2: return func.call(thisArg, args[0], args[1]);
case 3: return func.call(thisArg, args[0], args[1], args[2]);
}
return func.apply(thisArg, args);
}
export default apply;Expected behavior:
define("_apply", ["require", "exports"], function (require, exports) {
"use strict";
/**
* A faster alternative to `Function#apply`, this function invokes `func`
* with the `this` binding of `thisArg` and the arguments of `args`.
*
* @private
* @param {Function} func The function to invoke.
* @param {*} thisArg The `this` binding of `func`.
* @param {...*} args The arguments to invoke `func` with.
* @returns {*} Returns the result of `func`.
*/
function apply(func, thisArg, args) {
var length = args.length;
switch (length) {
case 0: return func.call(thisArg);
case 1: return func.call(thisArg, args[0]);
case 2: return func.call(thisArg, args[0], args[1]);
case 3: return func.call(thisArg, args[0], args[1], args[2]);
}
return func.apply(thisArg, args);
}
exports.__esModule = true;
exports["default"] = apply;
});Actual behavior:
define("_apply", ["require", "exports"], function (require, exports) {
"use strict";
/**
* A faster alternative to `Function#apply`, this function invokes `func`
* with the `this` binding of `thisArg` and the arguments of `args`.
*
* @private
* @param {Function} func The function to invoke.
* @param {*} thisArg The `this` binding of `func`.
* @param {...*} args The arguments to invoke `func` with.
* @returns {*} Returns the result of `func`.
*/
function apply(func, thisArg) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
var length = args.length;
switch (length) {
case 0: return func.call(thisArg);
case 1: return func.call(thisArg, args[0]);
case 2: return func.call(thisArg, args[0], args[1]);
case 3: return func.call(thisArg, args[0], args[1], args[2]);
}
return func.apply(thisArg, args);
}
exports.__esModule = true;
exports["default"] = apply;
});I've not been able to reproduce this in the TypeScript playground (currently running 1.8.2), which suggests a regression somewhere between 1.8.2 and 1.8.7.
Activity
sheetalkamat commentedon Apr 8, 2016
Oskar Segersvärd (@Victorystick)
Your annotation is for variable number of arguments:
May be using this instead is what you want to make args as array?:
mhegazy commentedon Apr 19, 2016
Sheetal Nandi (@sheetalkamat), we should not use the JSDoc comments for emit transformations. this is not what the user expects. the work around is fine, but we need to fix this for the compiler.
sheetalkamat commentedon Apr 19, 2016
Mohamed Hegazy (@mhegazy), noted, will work on the fix.
Adding testcase for function with rest param defined in jsDoc comment
Do not transform the emit of function with rest parameter unless decl…
Adding testcase for function with rest param defined in jsDoc comment
Do not transform the emit of function with rest parameter unless decl…