Pratap Singh Ranawat
Pratap Singh Ranawat

Reputation: 23

TypeScript - How to import module in other file

name1.name2.name3.ts file has one module

i.e.

/**
 * name1.name2.name3.ts
 */

 declare var PROCESS_ENGINE_BASEURL;
declare var CONST_HTTP_GET;

export module Name1.Name2 {

    export class Name3 {
       ...... 
       ......
      }
}

and in other file name1.name2.name5.name6.name7.ts

how can i import module Name1.Name2 from file name1.name2.name3.ts

both file are in same directory

I tried following

/**
 * name1.name2.name5.name6.name7.ts
 */

    import abc = require("name1.name2.name3"); failed

    import abc = require("name1.name2"); failed

    import abc = require("Name1.Name2"); failed

    import * as abc from "name1.name2.name3"; failed

    import * as abc from "name1.name2"; failed

    import * as abc from "Name1.Name2"; failed

Upvotes: 0

Views: 1517

Answers (1)

Amid
Amid

Reputation: 22382

I recommend you to stop mixing internal and external modules.

Therefore:

  1. Remove export module ... from Name3.ts
  2. Organize your code using file system: each module - its own file. Group modules in folders. In your case you will most likely end up with the following structure: Name1 / Name2 / Name3.ts, where Name1 and Name2 are folders.

After that - import Name3 any way you prefer, for example to import it from file (module) that is located in the same folder as Name1:

import {Name3} from './Name1/Name2/Name3';

Upvotes: 1

Related Questions