Skip to content
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

Name overload term/type module re-export as namespace #51109

Open
5 tasks done
jasonkuhrt opened this issue Oct 8, 2022 · 13 comments
Open
5 tasks done

Name overload term/type module re-export as namespace #51109

jasonkuhrt opened this issue Oct 8, 2022 · 13 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@jasonkuhrt
Copy link

jasonkuhrt commented Oct 8, 2022

Suggestion

πŸ” Search Terms

I wasn't sure what terms to be searching for this.

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Currently this is possible:

export type A = 1
export namespace A {
  export const foo = 2
}
import { A } from './a.js'

console.log(A.foo)
const A: A = 2 // type errror

However this is not possible:

export type A = 1
export * as A from './something/else/somewhere/else.js'

πŸ“ƒ Motivating Example

CleanShot 2022-10-08 at 08 39 55@2x

I want to be able to reuse a name for a type and namespace for more streamlined downstream usage.

πŸ’» Use Cases

All the same use-cases there already are today for namespace name overloading between terms/types.

@MartinJohns
Copy link
Contributor

I believe this to be a duplicate of #48764 (comment).

@jasonkuhrt
Copy link
Author

jasonkuhrt commented Oct 8, 2022

Thanks @MartinJohns. That issue was about the type import while mine is a suggestion that it should work like namespaces.

That issue was/is closed with the counter-example of importing a class.

What I'm saying here wouldn't be closed because of that counter example.

I'm merely saying it works with namespace and ought to work with native ESM modules now that we have them.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Oct 10, 2022
@cruhl
Copy link

cruhl commented Oct 25, 2022

I have been trying to get something like this working for literal years now.

It would dramatically help write better functional/Rust-module-style code.

@cruhl
Copy link

cruhl commented Oct 25, 2022

@jasonkuhrt check out this related issue/discussion:

#39865

@cruhl
Copy link

cruhl commented Oct 25, 2022

Another relevant discussion...

#48764 (comment)

@cruhl
Copy link

cruhl commented Oct 25, 2022

Good overview of the resulting friction this would alleviate here:
#48764 (comment)

@cruhl
Copy link

cruhl commented Oct 25, 2022

Here's a good example of the awkwardness this causes for users of @gcanti's fp-ts...
gcanti/fp-ts#1633

@cruhl
Copy link

cruhl commented Oct 25, 2022

Here is my dream syntax...
import * as User, type { User } from "./User";

@cruhl
Copy link

cruhl commented Oct 25, 2022

gcanti/fp-ts#1415
CleanShot 2022-10-25 at 13 48 16

@cruhl
Copy link

cruhl commented Oct 25, 2022

This problem won't be going away anytime soon, so I would expect it to continually pop up so long as issues keep getting closed...

CleanShot 2022-10-25 at 13 52 01

@cruhl
Copy link

cruhl commented Oct 25, 2022

Oddly, this halfway works, but fails to see export * as Point from "./Point"; correctly...

'Point' only refers to a type, but is being used as a value here.ts(2693)

CleanShot 2022-10-25 at 14 00 45
CleanShot 2022-10-25 at 14 01 30

@cruhl
Copy link

cruhl commented Oct 25, 2022

So this actually works...
CleanShot 2022-10-25 at 14 12 54
CleanShot 2022-10-25 at 14 13 04
CleanShot 2022-10-25 at 14 13 26

@cruhl
Copy link

cruhl commented Oct 27, 2022

So this actually works...

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants