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

Syntatic sugar for callable namespaces (functions as namespace) #23735

Closed
KSXGitHub opened this issue Apr 27, 2018 · 8 comments
Closed

Syntatic sugar for callable namespaces (functions as namespace) #23735

KSXGitHub opened this issue Apr 27, 2018 · 8 comments
Labels
Duplicate An existing issue was already created

Comments

@KSXGitHub
Copy link
Contributor

I have a suggestion: I love to write callable namespaces but I don't want to write its name twice (one for function, one for namespace), so I propose a syntax sugar for this:

export namespace myFunc {
  (x: number) => square(2 * x)
  export const square = (x: number) => x * x
}

This should be equivalent to:

export function myFunc (x: number) {
  return myFunc.square(2 * x)
}

export namespace myFunc {
  export const square = (x: number) => x * x
} 

Search Terms:
callable namespace syntax sugar

@AlCalzone
Copy link
Contributor

Related, but not sure if duplicate:
#21572
#15868

@KSXGitHub
Copy link
Contributor Author

KSXGitHub commented Apr 27, 2018

@AlCalzone I'm sure that they are not even related. The first is interface declaration which is a type rather than an object at runtime while functions are objects being created during runtime. As for the second one, I don't see how it's related to this at all.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 27, 2018

I would consider this a dupe of #15868.

#15868 is JS syntax, and no need to create new constructs if one already exists.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 27, 2018
@KSXGitHub
Copy link
Contributor Author

KSXGitHub commented Apr 27, 2018

@mhegazy Okay, I can see how #15058 would "solve" my problem but it has its own limitation:

  1. Namespace can contain type declarations, assignment cannot.

  2. Within a namespace, members can refer to each other without specifying namespace's name (e.g. when myFunc calls myFunc.square in the first code snippet: it called square(), not myFunc.square).

  3. Add quick fix for exporting "private" entities when imported indirectly for --declaration #15058 actually makes it more verbose/complicated than both two code snippets above: When I need to declare multiple namespace members, I need to write myFoo only once in the first snippet or twice in the second snippet but multiple times in Add quick fix for exporting "private" entities when imported indirectly for --declaration #15058, so it didn't solve my problem at all.

  4. Add quick fix for exporting "private" entities when imported indirectly for --declaration #15058 is harder for TypeScript compiler to check: What if I assign properties in a loop or from a different file?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 27, 2018

We already have a syntax for function + namespace; that already cover all your needs up there.

The request here is to add a new syntax for shorter declaration of function+namespace combo. So it covering all the features of the existing syntactic form is not the goal.

Here is my rational, First, we should not have two ways of declaring the same construct, unless there is a very good reason. Second, the proposal in the OP does not get us closer to a natural JS model. And it is one of our goals to be as close to JS as possible; we have already said we want to do the proposal in #15058, as it is identical to what you write in JS. that all said, if we were to add a new shorter form, it would be the one in #15058.

And as for 1, 2 and 3, you should use function + namespace declarations to achieve that. that also applies if you want to have multiple overloads for your function, or you want to declare the properties as const.

As for 4, no, that would not be allowed. only top-level assignments would be allowed with #15058.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@bluelovers
Copy link
Contributor

@mhegazy where is that syntax for function + namespace ?

can u post a link?

@AlCalzone
Copy link
Contributor

https://www.typescriptlang.org/docs/handbook/declaration-merging.html scroll down to about 2/3.

function buildLabel(name: string): string {
    return buildLabel.prefix + name + buildLabel.suffix;
}

namespace buildLabel {
    export let suffix = "";
    export let prefix = "Hello, ";
}

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants