You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
changed the title [-]Namespaces that assigned to types don't work with type namespace such as classes and interfaces[/-][+]Provide a way to alias namespaces[/+]on Aug 6, 2016
I've been thinking about this as well. We have var / const to alias a value, and type to alias a type, but no equivalent for namespace. We could even consider
namespacens=foo;
for establishing namespace aliases.
Reacted by falsandtru, Yahiko Uzumaki, Aluan Haddad, zhao5363, Tyler Sebastian, Wayne Crouch, Brian Frichette, Gerardo Lima, Michael Lehenbauer, hrmny and 20 more
Ryan Cavanaugh (@RyanCavanaugh) I know this is pretty old, but I'm currently trying to create better type defs for the popular node-forge library, which does a bunch of aliasing. E.g.:
Thus, I'm looking to do something where I can alias like:
declare module 'node-forge'{namespacepki{namespacersa{interfaceGenerateKeyPairOptions{}generateKeyPair(opts?: GenerateKeyPairOptions): KeyPair;}}// What I'd like to do:// namespace rsa = pki.rsa;// What I've tried:constrsa=pki.rsa;// Access to values from namespacetypersa=pki.rsa;// Trying to alias as type to access types (doesn't work)}
When using this lib, I should be able to do the following, but can't access types:
import{rsa}from'node-forge';// TS2503: Cannot find namespace 'rsa'constopts: rsa.GenerateKeyPairOptions={};// Works fine as aliased valuersa.generateKeyPair();
Reacted by Erik Philips, slizeray, Don Denton and Alex Spera
Reacted by Fico Prieto, Adam Cardenas, Simon Peters, falfia falfiya, Adam Smith, Vinícius Jardim, Lily Skye, Dmitry Maganov, Oster, Yusuf Tarık Günaydın and 1 more
Reacted by Fico Prieto, Simon Peters, Vinícius Jardim and Oster
so that the MyMod binding appears only in the type domain and hence would not conflict with the binding of the same name in the value domain, but that isn't possible because there is no way to alias namespaces.
Here's another use case - because of this problem ( #41567#41513 ) we can't use import for types from a non-module js file without typescript giving a faulty/broken output. This is being called 'working as intended'. But because of this """feature""", we can no longer simply import a namespace via import NamespaceAlias from "namespace"
To be fair to the "working as intended" defenders, it is true that we aren't actually truly importing the value. This was actually a workaround to the underlying problem of being unable to alias a namespace.
In other words, this is no longer valid TS code while not in a module file:
importAfrom'a';typeB=A.B;
The proposed workaround was to use import("a")
But this does not work
typeA=import("a")// Module 'a' does not refer to a type, but is used as a type here. Did you mean 'typeof import('a')'?
Sure:
typeA=typeofimport("a")typeB=A.B// 'A' only refers to a type, but is being used as a namespace here.
Thus, there is a need for a namespace alias, otherwise in every single reference to this namespace I'm going to have to replace A. with import("a").
Activity
[-]Namespaces that assigned to types don't work with type namespace such as classes and interfaces[/-][+]Provide a way to alias namespaces[/+]RyanCavanaugh commentedon Aug 6, 2016
I've been thinking about this as well. We have
var/constto alias a value, andtypeto alias a type, but no equivalent for namespace. We could even considerfor establishing namespace aliases.
falsandtru commentedon Aug 6, 2016
Thanks!! I agree its syntax.
related: #5220
falsandtru commentedon Aug 6, 2016
When extending namespaces, namespace merging is not good for immutability.
Source namespaces shouldn't be changed by another module. So I think, an
extendskeyword for namespaces is a good solution.saschanaz commentedon Aug 7, 2016
It seems this existing syntax just works?
zpdDG4gta8XKpMCd commentedon Aug 7, 2016
why won't we unify objects and namespaces instead of making them even more separated without a good reason?
related #8358
falsandtru commentedon Aug 7, 2016
Kagami Sascha Rosylight (@saschanaz) Oh, I didn't know its syntax. My problems were solved. Thanks for the great answer!!
zpdDG4gta8XKpMCd commentedon Aug 7, 2016
as was shown it works fine as long as you separate values/functions from types, unifying them is what we all can benefit from
RyanCavanaugh commentedon Aug 22, 2016
Looking to understand what the use cases for this are. Some real world code examples would be useful.
falsandtru commentedon Aug 22, 2016
My use case:
https://github.com/falsandtru/pjax-api/blob/v3.3.0/src/layer/domain/router/model/eav/entity.ts#L17
Near case:
https://github.com/falsandtru/localsocket/blob/v0.4.4/src/layer/domain/indexeddb/model/socket/data.ts#L18-L23
As a related case of #9102 :
RyanCavanaugh commentedon Sep 21, 2016
#11025 is also a good use case
RyanCavanaugh commentedon Sep 26, 2016
To explain what's going on in #11025 -- there's no way to alias in the namespace side of an elided module import into a merged identifier.
bfricka commentedon Oct 17, 2018
Ryan Cavanaugh (@RyanCavanaugh) I know this is pretty old, but I'm currently trying to create better type defs for the popular node-forge library, which does a bunch of aliasing. E.g.:
Thus, I'm looking to do something where I can alias like:
When using this lib, I should be able to do the following, but can't access types:
hiranya911 commentedon Jan 4, 2020
I have a similar use case that involves nested namespaces:
I'd like to expose everything in
Otheras a nested namespace ofParent. Something like:Today I achieve this by individually re-exporting the members of Other:
pgherveou commentedon Jun 24, 2020
stumbled upon this issue, it looks like this feature is supported now
see doc here https://www.typescriptlang.org/docs/handbook/namespaces.html#aliases
falsandtru (@falsandtru) if that answer your question you might want to close this issue
falsandtru commentedon Jun 24, 2020
See #10187 (comment) and below.
bakkot commentedon Jul 30, 2022
Ryan Cavanaugh (@RyanCavanaugh) I have another real-world use case, which is working around #48764 (comment).
Specifically, I have a library which has several classes which are gated behind an
initfunction, likeand as in the above example, the types for instances of the inner classes are exported from the library. The way you're supposed to use it is
but this doesn't actually work, because the
import typedeclaration conflicts with thelet MyModdeclaration.I would like to suggest that users write
so that the
MyModbinding appears only in the type domain and hence would not conflict with the binding of the same name in the value domain, but that isn't possible because there is no way to alias namespaces.ValeTheVioletMote commentedon Aug 2, 2022
Here's another use case - because of this problem ( #41567 #41513 ) we can't use import for types from a non-module js file without typescript giving a faulty/broken output. This is being called 'working as intended'. But because of this """feature""", we can no longer simply import a namespace via import NamespaceAlias from "namespace"
To be fair to the "working as intended" defenders, it is true that we aren't actually truly importing the value. This was actually a workaround to the underlying problem of being unable to alias a namespace.
In other words, this is no longer valid TS code while not in a module file:
The proposed workaround was to use import("a")
But this does not work
Sure:
Thus, there is a need for a namespace alias, otherwise in every single reference to this namespace I'm going to have to replace A. with import("a").
because this would, presumably, work fine