-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Suggestion: Allow ES6 export syntax in namespaces #39865
Comments
@mhegazy Blast from the past... There's finally an issue tracking this 😅 |
Here's more "in the wild" code I found showing the syntax noise needed to get around this... // This file is "./Claim/index.ts"
import * as Details_ from "./Details";
import * as Dashboard_ from "./Dashboard";
import * as Create_ from "./Create";
import * as Admin_ from "./Admin";
import { API } from "../API";
export type Claim = API.GetClaimQuery["getClaim"];
export namespace Claim {
export const Create = Create_;
export const Details = Details_;
export const Dashboard = Dashboard_;
export const Admin = Admin_;
} Here's what it could look like... import { API } from "../API";
export type Claim = API.GetClaimQuery["getClaim"];
export namespace Claim {
export * as Create from "./Create";
export * as Details from "./Details";
export * as Dashboard from "./Dashboard";
export * as Admin from "./Admin";
} Or even... import * as Details from "./Details";
import * as Dashboard from "./Dashboard";
import * as Create from "./Create";
import * as Admin from "./Admin";
import { API } from "../API";
export type Claim = API.GetClaimQuery["getClaim"];
export namespace Claim {
export { Details, Dashboard, Create, Admin };
} |
Here's another relevant discussion: #36684 (comment) |
Relevant Stack Overflow issue: https://stackoverflow.com/questions/47500855/exporting-imports-as-a-namespace-with-typescript |
Here's a repo someone created to demonstrate this issue, coincidently I used the same |
I just found out this works, but only in ambient contexts, which is bizarre: declare module "./Line" {
export namespace Line {
export { Point };
}
} |
This is also a feature I've been looking for. I've done this more times than I'd like to admit: import * as Handler_ from "./Handler"
export type Connection = ...
export namespace Connection {
export const Handler = Handler_
} Just to achieve const connectionHandler = Connection.Handler.create() |
Another way to achieve this without new syntax would be to allow type and value imports to have the same name, as is common in other scenarios... import * as Connection from "./Connection"; // namespace
import { Connection } from "./Connection"; // type |
You can also basically achieve this, but with huge drawbacks, using classes: import { Point } from "~/Point";
class Line {
static readonly Point = Point // works, but you can't do `export { Point }` in a namespace 👎
} |
Sorry if this is the wrong place to ask but namespace will still be a thing to be worked on? I usually encounter deprecation messages when I use it and Storybook sends an error message telling me to configure babel if I want to use it. Like @cruhl, I also found namespace to be a nice way to organize data and keep it more semantic // basic example
|
@andrewbranch I apologize if tagging you directly isn't the right move, let me know if that's the case, but I was wondering if you could provide some insight since you recently closed another related issue. This is still something I believe would be a useful change and I'm eager/willing to champion/help in whatever way I can. Any pointers for moving the issue forward? Thanks in advance! |
This issue has popped up again: |
If there's a good reason to not implement this feature I'd love to know about it. I feel as though I've seen this issue pop up a few times and it always gets closed with little-to-no explanation. I suppose if we could come up with a PR then maybe we could get some more in-depth feedback. |
Another use case is exporting values with keywords as names (like cruhl's workaround works with some modification: declare namespace WorkflowStepBuilder {
const _return: (value: string) => WorkflowStep<'return'>;
export {_return as return};
}
namespace WorkflowStepBuilder {
function _return(value: string): WorkflowStep<'return'> {
return {step: 'return', value: value}
}
WorkflowStepBuilder.return = _return;
} Quite a pain to write the generator for such scenarios |
For reference, this is how I'm doing this nowadays... |
TypeScript Version: [email protected]
Search Terms: namespace, import, export, re-export, module
Code
Expected behavior:
Exporting from a namespace should mirror ES6 module exports, i.e.
export { X }
,export { X } from "..."
, andexport * as NS from "..."
The lack of syntax for re-exports makes it very hard to use namespace/type/function merging abilities to represent nice APIs. Here's an example of what a module could look like if this was allowed...
Actual behavior:
TypeScript says "Export declarations are not permitted in a namespace," even though it actually respects the export declaration outside of the namespace.
Playground Link:
https://www.typescriptlang.org/play/#code/HYQwtgpgzgDiDGEAEBBJBvAUEnSIA8YB7AJwBcl4jgoKQkBeJAIhGaRCkutoG5MAvpkyhIsBMgBCGbLgLFyGVEgH8hmKjQr5GSSQDoU+kLyA
Related Issues:
#20273
This issue showcases some of the syntax gymnastics needed to get around this issue.
#4529
#4529 (comment)
More examples of verbose workarounds.
#38041 (comment)
@rbuckton Provides a nice example of what this could look like if enabled.
#38041 (comment)
My comment with another motivating example after the issue was closed.
The text was updated successfully, but these errors were encountered: