Repository navigation
Suggestion: Anonymous/Unnamed Modules #206
Description
Activity
RyanCavanaugh commented
on Jul 23, 2014 MemberMore actionsStating the obvious stuff to round this out:
exportinside this module is disallowed- Anonymous modules generate nothing in .d.ts files
- Symbol binding is unaffected compared to an unspeakable-named module
- No merging takes place across anonymous modules
RyanCavanaugh commented
on Jul 28, 2014 MemberMore actionsReady for discussion on this one.
I like this idea. I find it frustrating that I can't nest classes in IIFEs and always have to declare a named module. 👍 for this.
As an aside, will es6 classes be declarable inside an IIFE? I presume so? It would be nice if TypeScript classes could be nested in an IIFE so when migrating es6 code to TypeScript you weren't forced to migrate to modules if you didn't want to - same restrictions as specified by Ryan Cavanaugh (@RyanCavanaugh) obviously.
I sometimes find IIFEs to be a little too verbose, especially when they are nested. Does it make sense to have the followings to be interchangeably equivalent?
(function() { /*code*/ })() (() => { /*code*/ })() module { /*code*/ }John Reilly (@johnnyreilly) yes ES6 supports class expressions so you'd be able to nest class definitions in all sorts of places
Omid K. Rad (@omidkrad) - yes that's exactly what I was hoping for. Much cleaner to the eye. Though I'd quite like classes to be allowed in IIFE's anyway since it's going to be valid JavaScript when ES 6 finally ships. (And since one of TypeScript's original design goals was to be a superset of JavaScript I would guess that it makes sense for this to come along for the ride.)
Dan Quirk (@danquirk) - thanks for clarifying.
+1 on this issue. Is there a workaround in the meantime? I.e., is there a way for me to create classes, enums, etc. visible only within a single source file, without having to declare a module name that ends up as a (browser) global variable? I guess I could
deletethat variable afterward, but I was hoping for something cleaner.- addedDeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionand removedIn DiscussionNot yet reached consensusNot yet reached consensus
on Sep 19, 2014 RyanCavanaugh commented
on Sep 19, 2014 MemberMore actionsDiscussed in design meeting and declined. Relevant points:
- We'll have local types (class declarations at block scope) in the future, at which point an IIFE is usable instead
- You can just use a nonsense name as the module name with little risk of meaningfully altering the global scope
- Not enough value relative to the complexity it adds
Hi Ryan Cavanaugh (@RyanCavanaugh),
Understand that this is declined. Could you clarify what "local types (class declarations at block scope)" means with an example please? I may be being a bit dense but I'm not sure what this is.
I take it from what you've said that that classes will become usable inside IIFEs at that point?
RyanCavanaugh commented
on Sep 29, 2014 MemberMore actionsCertainly; the idea is that we will in the future allow code like this:
function f() { // or any function expression or any method body interface b { /* ... */ } class c { /* ... */ } // etc }
Fantastic! Thanks Ryan Cavanaugh (@RyanCavanaugh)
Yes, thanks to Ryan Cavanaugh (@RyanCavanaugh) and the TypeScript team for all the great work!
Awesome!
Just today I needed anonymous module and ended with UniqueName.
svicalifornia commented
on Jan 25, 2015 More actionsRyan Cavanaugh (@RyanCavanaugh) What complexity does this add, really?
This seems relevant to answer that: http://blogs.msdn.com/b/ericgu/archive/2004/01/12/57985.aspx
The issue is that it doesn't add much value on its own and it adds non-zero complexity that can't ever be removed.
Fascinating post Dan Quirk (@danquirk)! - Thanks for sharing.
Ryan Cavanaugh (@RyanCavanaugh) - Have local types been implemented in 1.5-beta? I'm trying
(function(){ class x{ } })()And still getting `TS1129 statement expected. class must be in a file or module context.
Richard Collette (@rcollette) local types are not implemented at present. They're scheduled for 1.6: https://github.com/Microsoft/TypeScript/wiki/Roadmap
sophiajt commented
on Jun 11, 2015 ContributorMore actionsJust to follow up - #3266, so this is now supported.
Reacted by WarrenAwesome!
Fantastic!
- locked and limited conversation to collaborators
on Jun 18, 2018
I would like to suggest the following: Why not have anonymous/unnamed modules? The following code snippet totally makes sense for me to compile:
The generated code should be wrapped in a closure function and not cluttering the global scope.
Also it would be nice to have a compiler option to put everything compiled in an anonymous module so that the global namespace is not cluttered.
The above snippet is with little modification from: https://github.com/proxy/gist.github.com/3916195
Thanks!