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

Typescript 'Namespace' not works #966

Closed
HealGaren opened this issue Aug 14, 2020 · 6 comments · Fixed by #1325
Closed

Typescript 'Namespace' not works #966

HealGaren opened this issue Aug 14, 2020 · 6 comments · Fixed by #1325
Assignees
Labels
Milestone

Comments

@HealGaren
Copy link

HealGaren commented Aug 14, 2020

Describe the bug
If I write code using the namespace spec in typescript, it is removed after transpiling.

handbook document link

Namespace should be able to have export function or export const variable, and etc..
And it can have runtime behavior.

Input code

namespace A {
    export function test() {
        console.log('test');
    }
}

A.test();

Config

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    }
  }
}

Expected behavior

Output of the same code compiled with tsc: typescript playground

"use strict";
var A;
(function (A) {
    function test() {
        console.log('test!');
    }
    A.test = test;
})(A || (A = {}));
A.test();

and swc:

A.test();

Version
The version of @swc/core: 1.2.19

Additional context

@HealGaren
Copy link
Author

HealGaren commented Aug 24, 2020

Isn't this a bug? Or is this spec not going to be implemented soon?

I'm looking forward to applying this awesome library to my project 👍

@kdy1
Copy link
Member

kdy1 commented Aug 24, 2020

It a feature which can't be supported by swc, but can be by spack.
However, I'm not sure if I have to implement this, mainly because modules are preferred over namespaces.

Side note: Implementation will be easy

@HealGaren
Copy link
Author

HealGaren commented Aug 24, 2020

Thank you for your answer 👍

May I ask why swc can't support this?

In my opinion, swc is a typescript compiler,
so I think it should support the syntax supported by typescript as much as possible...

Is it a design or technical issue of swc? Or doesn't it fit the purpose of swc?

@kdy1
Copy link
Member

kdy1 commented Aug 24, 2020

swc (and babel) works by file-by-file manner and namespace support (and const enums) requires reading multiple files.

Hmm... I noticed that it can supported if swc accepts a list of files or directory names.
But I'm not sure about it because namespace is not recommended.

There is a lint about it. https://palantir.github.io/tslint/rules/no-namespace/

@HealGaren
Copy link
Author

I don't think all use of namespaces is discouraged.
Most namespaces used for module purposes can be replaced with es module.

But like Java's inner class,
When designing code using namespaces, it is good to specify the contextual meaning.
example comment

The namespace is not deprecated.

It's also because typescript doesn't want to remove the existing spec,
Looking at the related babel and typescript discussions, I think there is a good reason that es module cannot replace.
it is necessary to apply selectively if possible.

@kdy1 kdy1 self-assigned this Jan 12, 2021
@kdy1 kdy1 added this to the v1.2.45 milestone Jan 12, 2021
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 25, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants