-
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
Support a way to export type X
and * as X
as namespace with values and types
#51975
Comments
It could also be nice to allow for: export * as Something from "./Something"
export type { Something } from "./Something" Given that it is already possible to have: type Something = number;
const Something = "hello"; within a single file as type level symbols and value level symbols can overlap without conflicts |
Totally, added as Option 3 (I suppose all 3 limitations lifted would be awesome) |
This looks like it's related to #37238, maybe? |
Related, but not duplicate, I believe. |
I’m not sure, but I think this might just be a bug. |
Probably is, even if that is solved though it I believe it won't be enough to resolve this proposal fully, the reason being import { Something } from "./prelude"
// this would work
export const myValue: Something<string> = Something.of("abc")
// this would not infer as Something<A>
export const myValue = Something.of("abc") |
It depends, I didn’t confirm this case, but afaik typescript will try to stick to an Alias when it is found “earlier”. That said, ideal would be each of the cases solved :) |
Never seen typescript do anything of the sort, the closest available symbol is usually taken not considering aliases. Try the same in a single file importing '* as S' and defining locally a new type alias, you'll see that TS doesn't pick it up (and it really shouldn't otherwise it would need to resolve all the aliases to know what points to what) |
Some related fixes have gone in TS 5.0 beta it seems: #37238 |
hi @andrewbranch this is great, thanks! export * as Something from "./Something"
export type { Something } from "./Something" It will complain
|
Yeah. Alias declarations do not merge with each other. That one is not intended to work. |
@andrewbranch why don't they? |
Those aren’t alias declarations; those are local declarations that are also exported. To be honest, I don’t know why alias symbols can’t merge, when an alias and a local can merge provided the meanings don’t conflict. But the restriction is deeply encoded into the symbol merging architecture and is definitely intentional. Changing that, even if there were no soundness or performance downsides, would be a huge lift. I can’t really give a justification that’s satisfying from a user perspective (though maybe there is one), but I can tell you after working on #50455, there are so many things implementation-wise that would be so much harder if aliases were allowed to merge with each other. |
Suggestion
🔍 Search Terms
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
I would like to be able to easily export a whole module including types and values, and at the same time export a type with the same name.
📃 Motivating Example
I want to have a single import of
Something
, which represents both a type, and a namespace containing all types and values of the Something module.Usage:
prelude.ts
:Option 1:
The limitation seems strange. Perhaps just a bug?
Option 2:
The limitation seems arbitrary. We can already export from namespaces. Why not allow * exports for convenience?
Option 3:
The limitation seems strange. As we already can create a type and value/namespace with same name, and export them together.
Something.ts
:💻 Use Cases
The current approaches either mean I have to use
Something.Something<string>
and also see thisSomething.
namespace in type errors and on hover in editor. It's verbose, and ugly.Or I need to work around the issue in either of two ways, both of them require manual syncing of files, or individual exports:
Workaround 1:
Manually create:
Prelude.d.ts:
Prelude.js:
Workaround 2: Manually alias all the exports in a namespace:
Prelude.ts:
The text was updated successfully, but these errors were encountered: