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

Add using namespace statement #53095

Closed
5 tasks done
ruochenjia opened this issue Mar 4, 2023 · 4 comments
Closed
5 tasks done

Add using namespace statement #53095

ruochenjia opened this issue Mar 4, 2023 · 4 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@ruochenjia
Copy link

ruochenjia commented Mar 4, 2023

Suggestion

πŸ” Search Terms

βœ… 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

Add a statement similar to using namespace NS; in C++, that allows to append members of an object/namespace/module to the global scope or function scope during compile time.

πŸ“ƒ Motivating Example

Using namespace in global scope

TypeScript:

module NS {
  var a = 0;
  var b = "a";
}

using namespace NS;
b = "b";
a++;

Compiled JavaScript:

NS.b = "b";
NS.a++;

Using namespace in function scope

TypeScript:

module NS {
  var a = 0;
  var b = "a";
}

function c() {
  using namespace NS;
  a++;
  b = "";
}

Compiled JavaScript:

function c() {
  NS.a++;
  NS.b = "";
}

Using namespace not allowed

module NS {
  var a = 0;
  var window = null;
}

// error here as 'window' is already declared
using namespace NS;

Extending another module

module A {
  var a = 0;
  var b = 1;
}

module B {
  using namespace A;
  var c = 10;
}

console.log(B.a); // 0
console.log(B.c); // 10

πŸ’» Use Cases

Currently, I have to use ModuleName.memberName statement to access a member declared inside a module. This doesn't have many effects when you only have a few references, but I have far more than the acceptable amount just in one file. It makes your function lines longer, affects the readability of the code, and wastes a lot of time on just on typing out the module name.

@MartinJohns
Copy link
Contributor

Duplicate of #11775. You should also check the viability checklist again. Point 3 is definitely wrong.

@IllusionMH
Copy link
Contributor

IllusionMH commented Mar 5, 2023

Why this one is checked?

This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)

How it's different from with?
In a sense "how it would avoid problems that usually caused by with"?

@Josh-Cena
Copy link
Contributor

Not to mention that using declarations are already taken by the resource management feature ^^

@fatcerberus
Copy link

fatcerberus commented Mar 5, 2023

Not to mention that using declarations are already taken by the resource management feature ^^

To be fair, C# uses it for both purposes. That said, yeah, this doesn't even come close to meeting the viability criteria.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints labels Mar 6, 2023
@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants