jayphelps / core-decorators

Library of stage-0 JavaScript decorators (aka ES2016/ES7 decorators but not accurate) inspired by languages that come with built-ins like @​override, @​deprecate, @​autobind, @​mixin and more. Popular with React/Angular, but is framework agnostic.
MIT License
4.52k stars 263 forks source link

TypeScript tests generate TypeError: arguments.concat is not a function #122

Closed BurtHarris closed 7 years ago

BurtHarris commented 7 years ago

@jayphelps : Here's something you might find interesting. It seems the use of ...arguments in src/private/utils.js cause TypeScript to generate problematic code that throws a TypeError. One fix I've found is it to change it to ...Array.from(arguments)

If I rename utils.js to utils.ts and jiggle the build configuration a touch, TypeScript catches the problem, reporting: src/private/utils.ts(27,34): error TS2461: Type 'IArguments' is not an array type. It's complaining because arguments isn't a real array!

The input code in context:

export function decorate(handleDescriptor, entryArgs) {
  if (isDescriptor(entryArgs[entryArgs.length - 1])) {
    return handleDescriptor(...entryArgs, []);
  } else {
    return function () {
      return handleDescriptor(...arguments, entryArgs);
    };
  }
}

TypeScript's output when targeting es5

function decorate(handleDescriptor, entryArgs) {
    if (isDescriptor(entryArgs[entryArgs.length - 1])) {
        return handleDescriptor.apply(void 0, entryArgs.concat([[]]));
    }
    else {
        return function () {
            return handleDescriptor.apply(void 0, arguments.concat([entryArgs]));
        };
    }
}
exports.decorate = decorate;

I thought for a while this might be a TypeScript bug, but now that I understand it, I don't think so. TypeScript wasn't originally designed to be a .js -> .js compiler, so I don't fault it for not finding this in --allowJs mode.