0

I have a interface definition

namespace shamethethrones.google { import Marker = google.maps.Marker; export interface MarkerColleciton { [key: string]: Marker; } }

but the issue is google.maps.Marker doesn't exist because it thinks I'm assuming shamethethrones.google.maps.Marker. How do I specify that I want the root namespace google and not shamethethrones.google?

Thanks

Jason Gallavin
  • 403
  • 4
  • 14

1 Answers1

1

You can try moving import outside of the namespace declaration:

import Marker = google.maps.Marker;

namespace shamethethrones.google {
    export interface MarkerColleciton
    {
        [key: string]: Marker;
    }
}
Amid
  • 21,508
  • 5
  • 57
  • 54
  • That works except then Marker becomes global and if I import somewhere else if will get a duplicate identifier error. – Jason Gallavin Apr 01 '16 at 23:47
  • Exactly. The reasoning why its not working otherwise you can see in this post: http://stackoverflow.com/questions/36249838/typescript-errors-when-referencing-files-with-similar-module-names/36252397#36252397 . Namespases are all about structuring global naming. My suggestion - forget about namespaces and use modules. – Amid Apr 02 '16 at 06:09
  • I've tried looking into modules but it seems the requiring is done at run time. I want my compiled typescript to be combined into one file so the browser only has 1 request to do instead of around 20 files I have now. Have I misunderstood modules? – Jason Gallavin Apr 02 '16 at 13:27
  • If you want to have only one file it is possible with modules - check out tools like systemjsbuilder, webpack, browserify. As a side note - loading 20 files separately is not nesessary a bad thing - you might benefit from shorter loading times as most likely you will not import all 20 files at once and their imports will be distributed 'over time' when user will be navigating through your program. – Amid Apr 02 '16 at 13:31
  • Some links for you to consider stop using namespaces: http://stackoverflow.com/questions/30357634/how-do-i-use-namespaces-with-typescript-external-modules, https://www.stevefenton.co.uk/2015/05/stop-mixing-typescript-internal-and-external-modules/, https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md – Amid Apr 02 '16 at 13:36
  • The thing I'm afraid of is the connecting time with the browser to initiate another request. I've read a bit on AMD and it seems nice because it asynchronously requires the files? If I were to choose one tool for this, what would you recommend? – Jason Gallavin Apr 02 '16 at 13:36
  • All of them will do the job and selecting any is more a matter of personal taste. My suggestion - try each and see what will feel more 'natural' for you to use. There many samples out there for each of them. – Amid Apr 02 '16 at 13:38
  • Is there any of them that feel like namespaces? Name spacing just feels natural to me because of OOP programming languages. Do you have to specify file paths for all of the modules you want to require? Also are all of these solutions ran at run time? I might experiment with the load times. Thanks! – Jason Gallavin Apr 02 '16 at 13:46
  • Wow there is so much on these module tool. It's so much, it's hard to make a decision on which one to use – Jason Gallavin Apr 02 '16 at 13:51
  • Those tool are compile time and can produce single file as you want. Regarding namespaces - I also had troubles getting used to them with background in c# mostly: http://stackoverflow.com/questions/30176289/using-external-modules-in-typescript – Amid Apr 02 '16 at 13:53
  • In that link you provided I read something concerning. "Ditch the module keyword. The file is a module already.". I'm not sure I want to put all of my classes of a module into one file. It would become messy and unusable. Does typescript have a solution for this? – Jason Gallavin Apr 02 '16 at 13:58
  • you should consider having one class per file. It is a good practice. For example java tries to enforce this rule. – Amid Apr 02 '16 at 14:07
  • Each file is considered to be a module isn't it? How would you keep the class files separate and yet stay within the same module? thanks for your help by the way, this has been a concept that I haven't dared to touch until today – Jason Gallavin Apr 02 '16 at 14:09
  • You are specifically talking about internal modules or external modules? – Jason Gallavin Apr 02 '16 at 14:19
  • Internal modules are called now namespaces. External modules are just modules now. The concept of organizing code in typescript is similar to the one of how you organize your data using filesystem. As analog of 'module' in your understanding you will have a folder. That is as close analog of C# namespace as I can get. Then inside each folder you have set of 'files - classes' that have something in common (they are in the same 'module-folder'). This way you organize your program using concept familiar to everyone who ever used filesystem. – Amid Apr 02 '16 at 14:23
  • I like that approach. I think I might use something like browserfy to handle this. are there any good tutorials that match what I am trying to do? There is a ton of information on modules that it is hard to find the information I want out of it. I am using visual studio. I wonder if there is a nuget package for it. – Jason Gallavin Apr 02 '16 at 14:25
  • Check this: https://medium.com/pixel-heart/typescript-gulp-browserify-less-web-server-livereload-2ccdcbc6be1d#.ptkphaufs. I think even if there is no nuget package, you can use npm as VS should support it by now. I prefer to stay away of VS and use VSCode instead though. – Amid Apr 02 '16 at 14:38
  • The project that me and a couple of classmates have already started using VS in this project. I usually use JetBrains IDE's for every other programming. This article looks. good. I will try and setup everything. I will probably add it to my repository for other classmates to use to but it might be a bit hard for them. I'm probably going to try it on a separate example project and once I learn the ropes I will try and integrate it into my current project. Thanks for your help. I will mark your answer since it is the closest that you will get in namespaces ;). You've given me great insight! – Jason Gallavin Apr 02 '16 at 15:06