-
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
Provide a way to alias namespaces #10187
Comments
I've been thinking about this as well. We have namespace ns = foo; for establishing namespace aliases. |
Thanks!! I agree its syntax. related: #5220 |
When extending namespaces, namespace merging is not good for immutability. namespace ns = foo;
// foo is overwritten
namespace ns {
export var bar;
} Source namespaces shouldn't be changed by another module. So I think, an // foo is not overwritten
namespace ns extends foo {
export var bar;
} |
It seems this existing syntax just works? namespace NS_A {
export class C {
}
}
namespace NS {
export import A = NS_A;
}
var C = NS.A.C;
type C = NS.A.C; |
why won't we unify objects and namespaces instead of making them even more separated without a good reason? related #8358 |
@saschanaz Oh, I didn't know its syntax. My problems were solved. Thanks for the great answer!! |
as was shown it works fine as long as you separate values/functions from types, unifying them is what we all can benefit from |
Looking to understand what the use cases for this are. Some real world code examples would be useful. |
My use case: Near case: As a related case of #9102 : class C {
}
namespace C {
export type T = void;
}
class D extends C {}
import D = C; // error |
#11025 is also a good use case |
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. |
@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.: forge.pki = forge.pki || {};
module.exports = forge.pki.rsa = forge.rsa = forge.rsa || {}; Thus, I'm looking to do something where I can alias like: declare module 'node-forge' {
namespace pki {
namespace rsa {
interface GenerateKeyPairOptions {}
generateKeyPair(opts?: GenerateKeyPairOptions): KeyPair;
}
}
// What I'd like to do:
// namespace rsa = pki.rsa;
// What I've tried:
const rsa = pki.rsa; // Access to values from namespace
type rsa = 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'
const opts: rsa.GenerateKeyPairOptions = {};
// Works fine as aliased value
rsa.generateKeyPair(); |
I have a similar use case that involves nested namespaces:
I'd like to expose everything in
Today I achieve this by individually re-exporting the members of Other:
|
stumbled upon this issue, it looks like this feature is supported now |
See #10187 (comment) and below. |
@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 export interface Foo {
method(): void
};
interface FooCtor {
new(): Foo
};
export function init(): { Foo: FooCtor } {
class Foo { method() {} }
return { Foo };
} and 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 import { init } from 'my-module';
import type * as MyMod from 'my-module';
let MyMod = init();
let foo: MyMod.Foo = new MyMod.Foo(); but this doesn't actually work, because the I would like to suggest that users write import type * as _MyMod from 'my-module';
namespace MyMod = _MyMod; so that the |
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: import A from 'a';
type B = A.B; The proposed workaround was to use import("a") But this does not work type A = import("a") // Module 'a' does not refer to a type, but is used as a type here. Did you mean 'typeof import('a')'? Sure: type A = typeof import("a")
type B = 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"). because this would, presumably, work fine namespace A = import("a") |
TypeScript should have a way to embed (type) namespaces. In the following case, assigned (embeded) namespace NS.A should have a C type.
TypeScript Version: master
Code
Expected behavior:
Actual behavior:
The text was updated successfully, but these errors were encountered: