Home > front end >  How to include functions in namespace when don't have module?
How to include functions in namespace when don't have module?

Time:07-04

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

CodePudding user response:

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

CodePudding user response:

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

  • Related