Repository navigation
(0,eval) produces TS2695 #12978
Description
Activity
(0,eval)is a hack, you can just use your own global eval like:const myEval = eval; myEval(`var x = 10`);
I'm personally fine with TS erroring out on this. It is a really edgy case.
Reacted by Timothy Soehnlin, Sharon (Sean) Rolel, Marin Marinov and Aluan HaddadCalling something hack or edge case isn't relevant.
Infinityis real proper edge case, yet it produces no error:1+Infinity.(function(){ })()is a definite absolute hack, yet no errors pop up.What's relevant is the usage of a given pattern.
(0,eval)is widely used, even in pretty much cutting edge libraries:Widely-used well-known googlable "hacks" should not trigger errors. I think this check was added recently — I don't think it generated this noise in 2.0.6. It's a regression and may cause undue friction for migrating code from JS.
mihailik
SystemJSis a module loader, it's meant to evaluate code so it's the one place where you might find this hack outside of transpiled code.I agree there is a bar here of what we want to honor or not.
(function() {})()for scoping is hardly a hack in "idiomatic JS". Relying on whether or notevalis referenced directly or indirectly for defining global variables seems like a much much bigger stretch.Reacted by Marin Marinov and Aluan Haddad- addedIn DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
on Dec 16, 2016 RyanCavanaugh commented
on Dec 16, 2016 MemberMore actionsAllowing strictly
evalas a special case right operand here seems fine.Reacted by mihailik and Sean VieiraTotally agree with
evalbeing the only special exception here.Certain type of code relies on
eval: binding frameworks, dynamic code transforms, loaders and so on. Indirect(0,eval)is an important feature in such cases.Couple more examples:
aluanhaddad commented
on Dec 17, 2016 ContributorMore actionsTroy Gerwien (@yortus) thanks for the providing the context. It seems a bit hand-holdy given that it's not a type level rule and that people can generally be expected to know the syntax for control flow constructs. The comma operator is definitely an easy thing to get wrong and probably something I would otherwise want to ban with a linter but not necessarily at the language level.
Reacted by mihailik and Jarrod DixonUsing
voidoperator disables the check, so you can use(void 0,eval)('var x = 10');- addedHelp WantedYou can do thisYou can do thisand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Jan 24, 2017 - added this to the This milestone has been deleted milestone
on Jan 24, 2017 RyanCavanaugh commented
on Jan 24, 2017 MemberMore actionsAccepting PRs to allow strictly the identifier
evalas the right-side comma operand without errorReacted by Basarat Ali Syed and mihailikReacted by Marin Marinov- addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Feb 27, 2017 - locked and limited conversation to collaborators
on Jun 19, 2018
TypeScript Version: 2.1.4
Expected behavior:
Compile fine.
Actual behavior:
error TS2695: Left side of comma operator is unused and has no side effects.
See also at TS Playground.
(0,eval)is a well-used convenience syntax for invokingevalwith a global scope (as opposed to the scope of the caller).One possible workaround is:
(eval||null). But that's longer and most of all — hard to Google the reasoning behind. Whereas(0,eval)is easy to find a detailed explanation for.Please make it not error.