Skip to content

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

Closed
omidkrad opened this issue Jul 23, 2014 · 22 comments
Closed

Suggestion: Anonymous/Unnamed Modules #206

omidkrad opened this issue Jul 23, 2014 · 22 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@omidkrad
Copy link

Suggestion moved over from codeplex.

I would like to suggest the following: Why not have anonymous/unnamed modules? The following code snippet totally makes sense for me to compile:

module {
  class Simple {

    constructor(public name: string) {}

    greet(who: string) {
      return "Greetings "+ who +", I'm "+ this.name +"!";
    }

    static main() {
      var s = new Simple('Flynn');
      console.log(s.greet("Program"));  
    }

  }

  Simple.main()
}

The generated code should be wrapped in a closure function and not cluttering the global scope.

(function () {

 // ...

})();

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!

@RyanCavanaugh
Copy link
Member

Stating the obvious stuff to round this out:

  • export inside 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
Copy link
Member

Ready for discussion on this one.

@johnnyreilly
Copy link

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.

@omidkrad
Copy link
Author

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*/ }

@danquirk
Copy link
Member

@johnnyreilly yes ES6 supports class expressions so you'd be able to nest class definitions in all sorts of places

@johnnyreilly
Copy link

@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.

@msridhar
Copy link

+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 delete that variable afterward, but I was hoping for something cleaner.

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed In Discussion Not yet reached consensus labels Sep 19, 2014
@RyanCavanaugh
Copy link
Member

Discussed 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

@johnnyreilly
Copy link

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?

@RyanCavanaugh
Copy link
Member

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
}

@johnnyreilly
Copy link

Fantastic! Thanks @RyanCavanaugh

@msridhar
Copy link

Yes, thanks to @RyanCavanaugh and the TypeScript team for all the great work!

@omidkrad
Copy link
Author

omidkrad commented Oct 8, 2014

Awesome!

@NN---
Copy link

NN--- commented Jan 15, 2015

Just today I needed anonymous module and ended with UniqueName.

@svicalifornia
Copy link

@RyanCavanaugh What complexity does this add, really?

@danquirk
Copy link
Member

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.

@johnnyreilly
Copy link

Fascinating post @danquirk! - Thanks for sharing.

@rcollette
Copy link

@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.

@danquirk
Copy link
Member

@rcollette local types are not implemented at present. They're scheduled for 1.6: https://github.com/Microsoft/TypeScript/wiki/Roadmap

@sophiajt
Copy link
Contributor

Just to follow up - #3266, so this is now supported.

@johnnyreilly
Copy link

Awesome!

@omidkrad
Copy link
Author

Fantastic!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

9 participants