-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Add new syntax to import namespaces #2956
Comments
I am assuming this means generating the AST from the source file.
How does the compiler currently deal with types defined outside of the file? What problem does |
There are two workflows that the TypeScript compiler supports today:
In the full program compilation mode, it is not an issue. it is just an extension on how we do namespaces today. but in the second it is going to work. So something like: // file: a.d.ts
declare namespace A {
function helper(): void;
} // file: b.ts
import * from A;
helper(); and expect the output to be: // file: b.js
A.helper(); would not work if the compiler only looked at We have the option to enable it only for the first mode. the problem here is forking the language into two dialects, where some things work in one but not the other. |
@mhegazy Thanks for the info. That's something I was trying to work out in my head as to how it would work without forking the language and I'm not really sure that there is a good way to do it. I guess the argument could be that with namespaces the /// <reference comment essentially is a part of the language and so it is already forked. Maybe continuing down that forking paradigm isn't a great idea. |
@mhegazy Do you know, if this has been given any further consideration? :) |
It keeps comming up, but no more use cases/proposals other than what is discussed earlier in this issue. |
+1 I would love a feature like this. As my code files grow and I use decorators more and more my |
+1 |
I've been writing more TypeScript lately after some time not using it. The import statements are, by far, the most painful part of the language.
I don't see any reason why TypeScript requires an alias. It knows all the types coming out of that file; therefore it can statically check for conflicts with other types that have been imported. If someone has some additional module loaded at runtime outside of this- well then they're already stepping outside the type system and that's their own responsibility to make sure there are no conflicts.
People have tried to simulate this with hacks like so-called "barrel files", but those cause all kinds of problems with circular references when used with certain tools. The solution is first-class compiler support for actual project files. It looks like you guys are exploring other import changes so I strongly urge you to explore fixing this painful area of the language as well. The current status quo are brittle file references that break if you move anything, having to constantly update import statements to add additional types, and overall a ton of wasted time. Please improve this awful situation. |
I don’t remember exactly what was going through my head when I first opened this issue. TypeScript is just a superset of JavaScript. So, there are no namespaces. The namespace feature in TS is just an artifact of being around before the ES module spec stabilized (pretty sure).
|
Bottom line, I want import statements to be as simple as: import * from MyProject.Components;
import * from MyProject.Utilities; Where are MyProject.Components located? My file shouldn't know or care where. The project file and compiler should take care of that. Hardcoding paths guarantees breakages upon refactoring (which is exactly what TypeScript was designed to help avoid). "Barrel files" are a hack people came up with to avoid this, but they have many problems themselves. |
@MgSam I understand how this could be useful, but it runs directly counter to TypeScript's design goal of being just ECMAScript with type annotations. In the early days of the project we added some extensions outside of the type system (e.g. enums and namespaces), but at this point we're firmly committed to tracking ECMAScript and TC39 in the core language. That's really the place you should advocate for this feature. |
@ahejlsberg I appreciate that being "JavaScript with types" is a design goal, but I think in reality TypeScript has never been just that. It originally was a place to try out features long before they ever came near to being part of ECMAScript, the type system just being the most prominent example. Even the most basic features like classes and lambdas were in TypeScript long before it was ever guaranteed to be added to ECMAScript. I remember in some of your first talks about TypeScript how you mentioned that you were adding these things because hopefully they'll be added to JS, but who knows if that'll ever happen. More recently, you guys have even done more non-type related stuff like down-level async/await emit, React support, and decorators. Adding new stuff into TypeScript is valuable not just for users immediate productivity, but also, by your own words, helps inform the design of the features when they do eventually (some day) come up in the ECMAScript committee meetings. With WebAssembly, the web will soon be a much more crowded place with many other languages able to compile into WebAssembly and run in a browser (including C#!). JavaScript will become less and less relevant- tying yourself to the politics and glacial progress of a standards committee is the last thing you should be doing now. Waiting on the ECMAScript committee to make decisions is hamstringing the evolution of the language, hurting users, and ultimately risking the continued relevance of TypeScript. I think exhibit one to back up that claim is the safe navigation operator. It's far and away the most popular feature request ever suggested for the language, has been asked for for at least four years, would yield massive benefits to writing the language, is relatively simple to implement (as far as language features go) and yet is not implemented because its stuck in standards hell with TC39. Even your friends on the Angular team gave up waiting and implemented the operator themselves. And speaking of the Angular team, you should reflect on how you barely avoided having them fork and make their own typed-superset of JavaScript. And you did this in large part by being willing to add decorators prior to their inclusion in ECMAScript. You guys failed to accomplish the same with Facebook. Having a rule that you cannot organically add non-type related features practically guarantees that this will inconvenience some other company (large or small) eventually and result in even further fragmentation of the ecosystem as they too make their own forks. I really hope the language design team reassess this new mantra of "we're not adding anything until it's approved by TC39". It's not doing you guys or your users any favors. |
Thanks for the feedback - we're continually evaluating the right balance between tracking TC39 and the convenience of the language. |
After 5 years of TypesScript and a 100K plus code-base I truly believe that the namespace approach is superior to (external) modules. The world will come to this realisation one day. 😞 🔫 |
Opening this separate issue as suggested by @mhegazy in the PR mentioned below. I'm not the most informed on this issue but I am opening it because I do not want it to get lost/forgot.
Quoting @mhegazy here:
Now that TypeScript has renamed internal modules to namespaces and has the namespace keyword (PR #2923) it would be nice to be able to import a namespace and then have access to the types defined on that namespace. Would this essentially just be a new syntax to do what
/// <reference path>
does?What are the obstacles and concerns for this feature?
The text was updated successfully, but these errors were encountered: