Say you need to use a typescript/node library, inside an internal module, which is spread across multiple .ts files.
ApiRepositoryHelper.ts
import * as requestPromise from "request-promise";
module ApiHelper {
class ApiRepositoryHelper extends DataRepositoryHelper {}
}
DataRepositoryHelper.ts
module ApiHelper {
class DataRepositoryHelper {}
}
As soon as I add the line:
import * as requestPromise from "request-promise";
, the DataRepositoryHelper
becomes inaccessible. What is the solution?
Observations:
Correct me if I'm wrong:
- Typescript internal modules can be split across multiple files.
- Typescript external modules can not.
- If you add a reference to an external module (say jQuery) outside the scope of your internal module (called 'A'), within the same file, you can use JQuery within A. But then your internal module 'A' becomes inaccessible across the rest of the project.
- If you have an external module, you can reference other external modules.
- You cannot reference the external module within the internal module scope, you will get "Import declarations in a namespace cannot reference a module."
I've read: Correct way to reference modules from other modules in Typescript.
I've also read:
Internal Module does not work if it has an import statement before it, but I don't agree with:
you can organise your code really well without internal modules
Simply because,
module ApiHelper {
class myClass {}
}
is neater than,
import {Model} from "../../../lib/interfaces/model/Model";
import {List} from "../../../lib/classes/helper/List";
import {Serializable} from "../../interfaces/model/Serializable";
import {DataRepository} from "../../../lib/interfaces/data/DataRepository";
import {ModelFactory} from "../../interfaces/model/modelFactory";
import {DefaultApiParser} from "./DefaultApiParser";
import {ApiItemParser} from "./ApiParser";
//Insert two hundred other imports here...
export class MyClass{}
Also, using modules is superior, in my opinion, because you are not using the filesystem to reference your classes (this is the recommended way?!) - but just splitting them up and not caring where they are.