Using an external typescript library inside an internal typescript module

Suppose you need to use the typescript / node library inside an internal module that extends to 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"; , DataRepositoryHelper becomes unavailable. What is the solution?

Remarks:

Correct me if I am wrong:

  • Typescript internal modules can be divided into several files.
  • Typescript external modules cannot.
  • If you add a link to an external module (say jQuery) outside the scope of your internal module (called "A"), you can use jQuery within A in the same file. But then your internal module "A" becomes unavailable in the rest of the project .
  • If you have an external module, you can reference other external modules.
  • You cannot reference the external module in the internal area of ​​the module, you will get "Importing declarations in the namespace cannot reference the module."

I read: The correct way to reference modules from other modules in Typescript .

I also read:

The internal module does not work if there is an import statement in front of it , but I do not agree with:

you can organize your code well without internal modules

Just because

 module ApiHelper { class myClass {} } 

is tidier than that

 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{} 

In addition, the use of modules is superior, in my opinion, because you do not use the file system to reference your classes (this is recommended!), But just separate them and don’t care where they are.

+2
javascript typescript
Feb 23 '16 at 16:01
source share
1 answer

Trust us! (those who tried and burned themselves), modules/namespaces best avoided when you can.

Using them, you can solve many uninteresting problems, such as

  • compile temporary file ordering ( How to create the correct file order using typescript? )
  • circular links ( Circular dependency issue with Typescript, CommonJS and Browserify )
  • etc.

Also "neat" is controversial

  • If this can make you feel better than Java developers - a few years before Typescript even existed - many will import at the top of their files, and I hope their lifespan is not shorter than that of Javascript developers ( being one of them). More seriously, some IDEs, such as Webstorm (12 EAP), automatically handle the import for you and even stack them in an editor so that they do not "pollute" the presentation of your code.

  • What you are doing is somehow import * , rather than importing what you require, is controversial in terms of code design.

Isolation is generally better with external modules.

Now suppose your code is split into two: an API and an implementation, and you want to immediately import all the API definitions in the implementation classes to avoid multiple import statements.

In the API section, you can create an api.ts file that relays all ie

 export * from 'interfaces/model/Model' export * from 'interfaces/model/Interfaces' export * from 'interfaces/model/Serializables' ... 

Then, in the implementation classes, simply import a single api.ts file into the namespace, for example:

 import * as api from '../api/api.ts' 

Then you can access all exported APIs like api.List , api.MyModel , etc.

But since you are at it, why not go all the way and create an external module to define your APIs? :)

Library creation

Unfortunately, at the moment I can not find the final resource.

TL; DR to create a library:

  • create index.ts , which re-exports all the definitions you want to open in your library.
  • compile library files with the --declaration flag to automatically generate typing
  • add a typings entry to package.json , pointing to the generated index.d.ts file

To use the library:

  • put it in the node_modules folder like any other library
  • just import * as mylib from 'mylib' : it automatically imports all exported definitions and type characters

There is a brief mention here

You can also see the Library and Usage paragraphs of what I wrote there.

If I find a better resource, I will update this answer

+1
Feb 23 '16 at 21:01
source share



All Articles