0

I am trying to understand the namespaces and modules in TypeScript.

Please see the sample code:

namespace ModuleOne.ModuleTwo.ModuleThree{
       export class SomeClass{
           constructor(){};
            someVariableOne:any;
            someVariableTwo:any;
            someFunction= function(){someFunctionCode};
      };
}

But while using this SomeClass.

ModuleOne.ModuleTwo.ModuleThree.SomeClass.someFunction();

I am getting this error as:

Cannot find name 'ModuleOne'

Please let me know If I'm doing something wrong or there is some other approach that I need to follow.

1 Answer 1

1

Your code has a different error:

Property 'someFunction' does not exist on type 'typeof SomeClass'

(your code in playground)

The reason is that someFunction is a property of the class instance, and not a static method.
It should be:

let instance = new ModuleOne.ModuleTwo.ModuleThree.SomeClass();
instance.someFunction();

Or if you want a static method:

namespace ModuleOne.ModuleTwo.ModuleThree{
       export class SomeClass{
           static someFunction() { }

           constructor(){};
            someVariableOne:any;
            someVariableTwo:any;
      };
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is not the error that I'm getting, I even tried it in playground, did not give me any error. I'm using VS code. But having the error of cannot find name.
Well, the playground link in my answer does have a compilation error.. Unless of course the code in your question isn't the code you're trying to compile. Also, what version of typescript are you using?

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.