0

How to include functions in namespace / module when don't have module?

example: I have a namespace:

namespace Java {}

in typescript, usually when we need a function in namespace or module, we add 'export':

namespace Java {
    export function version() {
        return "java8";
    }
}
namespace JavaScript {
    function JavaVer() {
        return Java.version();
    }
}

But if I set module to none:

// tsconfig
"module": "none",

If module is none, I cannot use import / export:

website/sourcecode/main.ts:17:21 - error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.

17 export function version() {}

so what can I do?

image: enter image description here

1
  • Why do you set { "module": "none" } in the TSConfig? Commented Jul 4, 2022 at 0:54

2 Answers 2

0

You can use a plain object:

TS Playground

const Java = {
  version () {
    return "java8";
  },
};

const JavaScript = {
  JavaVer () {
    return Java.version();
  },
};

const vJ = Java.version(); // string
const vJS = JavaScript.JavaVer(); // string
console.log(vJ === vJS); // true

Sign up to request clarification or add additional context in comments.

Comments

0

It was answered in here: It's actually "can't have any imports or exports at the top level". Imports and exports inside namespaces are fine because they don't export anything from .ts file at runtime, and as long as file remains 'not a module' module=None works fine. Long explanation here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.