question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Seemingly wrong arguments object when both es6 and commonjs options are present

See original GitHub issue

TypeScript Version: 2.4.2

Code

export const foo = (bar: number) => {
    console.log(arguments)
}

Expected behavior:

The code either produces errors or prints the arguments object of foo consistently in all targets and module types

Actual behavior:

  • When the module is built with tsc bug.ts command line, error TS2496: The 'arguments' object cannot be referenced in an arrow function is produced and no code is generated.
  • With tsc -t es6 bug.ts it builds an es6 module and the code prints the arguments of foo at runtime
  • With tsc -t es6 -m commonjs the generatesd commonjs module prints a strange object with 4 arguments, which seems to be an implementation detail of commonjs modules in node.js.

So I’d like at least to produce a warning in the third case, because I hit this problem in my program and the resulting bug was hard to locate.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Aug 18, 2017

We introduced the error rather than alias arguments because early versions of TS (created before the arrow function spec was finalized) did allow use of arguments in arrow functions, so changing to capturing the outer arguments value instead would have changed the behavior of the emitted code (extremely subtle and dangerous).

The plan was to introduce this error, wait “a while”, then see if anyone cared enough for us to bother implementing the aliasing (seems like the answer is ‘no’).

1reaction
vladimacommented, Aug 17, 2017

I would argue that TS should error here (assuming that initial source file is ES6 module). Per spec: ModuleDeclarationEnvironmentSetup sets lexical environment for the module to be NewModuleEnvironment parented by GlobalEnvironment and non of these will have bindings to arguments object (that is created only during FunctionDeclarationInstantiation). The fact that in cjs case instantiation of the module happens inside some function call and arguments is available is an implementation details which should not be applied to behavior described in spec.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocha tests with 'esm' support for native ES6 modules
UPDATE: I think the reason for the error is a conflict between --require esm option in mocha arguments and other methods of indicating...
Read more >
Command-line parameter to force "module" instead ... - GitHub
I'm aware that I can name a file with .mjs or set "type": "module" in package.json. I'm also aware of the --input-type=module CLI...
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
In ESM scripts, import and export are part of the language; like CJS, they have two different syntaxes for named exports and the...
Read more >
Understanding (all) JavaScript module formats and tools
UMD for both AMD (RequireJS) and CommonJS (Node.js) ... If the current runtime, like an old browser, does not support ES6 syntax, ...
Read more >
How To Use ES6 Arguments And Parameters
max() method doesn't support arrays; it accepts only numbers. When an array is passed to the Math.max() function, it throws an error. But...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found