-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Suggestion: Anonymous/Unnamed Modules #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Stating the obvious stuff to round this out:
|
Ready 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 @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?
|
@johnnyreilly yes ES6 supports class expressions so you'd be able to nest class definitions in all sorts of places |
@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.) @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 |
Discussed in design meeting and declined. Relevant points:
|
Hi @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? |
Certainly; 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 @RyanCavanaugh |
Yes, thanks to @RyanCavanaugh and the TypeScript team for all the great work! |
Awesome! |
Just today I needed anonymous module and ended with UniqueName. |
@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 @danquirk! - Thanks for sharing. |
@RyanCavanaugh - Have local types been implemented in 1.5-beta? I'm trying
And still getting `TS1129 statement expected. class must be in a file or module context. |
@rcollette local types are not implemented at present. They're scheduled for 1.6: https://github.com/Microsoft/TypeScript/wiki/Roadmap |
Just to follow up - #3266, so this is now supported. |
Awesome! |
Fantastic! |
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://gist.github.com/3916195
Thanks!
The text was updated successfully, but these errors were encountered: