Consider the following strange behavior I noticed in one of my projects:
async function hello() {
return arguments;
}
When TypeScript's compilation target is set to es3 or es5, the above file fails to compile with the following error:
error TS2522: The 'arguments' object cannot be referenced in an async function or method in ES3 and ES5. Consider using a standard function or method.
2 return arguments;
~~~~~~~~~
However, with a higher compilation target (I've tested es2017 and esnext) there is no error.
What is it about the arguments keyword that prevents it from being used in async functions when TypeScript's compilation target is set to es3 or es5?
A few notes:
- When replicated in modern JavaScript, this function does not throw an exception
- This behavior can only be replicated in
asyncfunctions
My Hypothesis
I suspect that because Promise needs to be polyfilled in es3 and es5, the polyfill cannot support arguments because it is dependant on the function callee.
Further reading: ES5.1 Spec § 10.6 Arguments Object
return argumentsthan have a look at what the transpiler generatesargumentsorthisin an arrow function.arguments.arguments) and not have users trip over error messages :-)