Seemingly wrong arguments object when both es6 and commonjs options are present
See original GitHub issueTypeScript 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 offoo
at runtime - With
tsc -t es6 -m commonjs
the generatesdcommonjs
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:
- Created 6 years ago
- Comments:7 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We introduced the error rather than alias
arguments
because early versions of TS (created before the arrow function spec was finalized) did allow use ofarguments
in arrow functions, so changing to capturing the outerarguments
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’).
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 andarguments
is available is an implementation details which should not be applied to behavior described in spec.