3 ms·
No they are not needed. See these and many similar statements/posts. https://michelenasti.com/2019/01/23/is-typescript-namespace-feature-deprecated.html https
by nosianu 3y ago
No they are not needed.
See these and many similar statements/posts.
https://michelenasti.com/2019/01/23/is-typescript-namespace-feature-deprecated.html https://michelenasti.com/2019/01/23/is-typescript-namespace-...
https://stackoverflow.com/questions/64495793/typescript-namespaces-to-be-discontinued https://stackoverflow.com/questions/64495793/typescript-name...
https://github.com/microsoft/TypeScript/issues/30994#issuecomment-492017219 https://github.com/microsoft/TypeScript/issues/30994#issueco... (link to a specific comment)
> Namespaces are probably never going away and, simultaneously, you probably shouldn't use them, since better, more standard and modern patterns exist through the use of modules. If you have code that you feel needs to use a namespace, more power to you and go for it, but most of the time you don't need one, as we now have a different conceptual organizational model recommended for large-scale JS (modules). That's why @DanielRosenwasser (Program Manager of TypeScript) said namespaces are not a huge loss - most new TS code should probably never need to use them, and should think hard about if they really need to.
Sure, you can use them, enums as well still provide a few more options than object literals, and since those features are in TS anyway and will remain like everything in JS always remains for backwards compatibility... - but they are not needed.
There even is a sentence in the TS documentation (under Namespaces and Modules) that starts with "If you’re converting a program from namespaces to modules,..." (see https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html#needless-namespacing https://www.typescriptlang.org/docs/handbook/namespaces-and-...)
This is distorting my post's point, which was Typescript is ECMAscript, and I did mention the two big exceptions, and I said they are not needed - which is just truth. They were necessary before ES 2015, which is the reason they exist to begin with. Other than that, Microsoft's explicitly stated goal is to keep Typescript perfectly aligned with ECMAScript, and to only add types.
Now that the language has all the minimum features they don't make any more changes that require actual transpiling because the code itself - not the types - is "Typescript only" and not part of ECMAScript. They will, however, leave in anything that is already there, just like ECMAScript does, because backwards compatibility trumps all.
Also note that you can have types-only namespaces that only contain type definitions, which are entirely different: They fall exactly under what I said, types are just removed in the .js file generation step. Those kinds of "namespaces" don't lead to any executable code.
Not to mention that you could easily just write the tiny bit of JS yourself instead of using the namespace keyword, because namespaces are simply named JavaScript objects in the global namespace. -- This is all it does: https://www.typescriptlang.org/play?noUnusedLocals=true&noUnusedParameters=true&preserveConstEnums=true&target=99&jsx=0&pretty=true&q=283#code/HYQwtgpgzgDiDGEAEARCYD2A5Aykg3gLABQSS8GwUGANhAHQ0YDmAFACwBMAlANwkBfEkA https://www.typescriptlang.org/play?noUnusedLocals=true&noUn...
So if you want the same thing as TS namespaces with just JS code, all you need is a global object and then assign stuff to it, from one or from several modules.
- duped 3y agoThe comment below the one you linked is why namespaces remain useful: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-namespaces-with-classes-functions-and-enums https://www.typescriptlang.org/docs/handbook/declaration-mer... It's a stretch to say "they're not needed." Like the comments in the threads you linked, they are usually not necessary for code written with esm modules in mind. However, modules cannot express the same semantics that namespaces can, and if you need those semantics for your program, you need namespaces. Similarly, enums are not semantically equivalent to object literals with const fields, and I think it's kind of absurd to say that one of the most useful things in the TS type system is "not needed.
- nosianu 3y agoYES! I use that myself! It's the only way to extend an existing type from different modules. Use an interface and make use of the automatic declaration merging, and for that to be triggered it needs to be in the same namespace. Which since modules are each their own namespace needs the "namespace" declaration. But that is for TYPES and does not produce any JS code! > Similarly, enums are not semantically equivalent to object literals with const fields I know and I mentioned it! That still does not make them needed! You can use them if you want. As I mentioned. > I think it's kind of absurd to say that one of the most useful things in the TS type system is "not needed. I on the other hand think it's absurd to pretend to have a serious discussion when you claim that enums are "one of the most useful things" in TS. Some very minor syntactic sugar add-on as one of the main points of type checks???