How to link Typescript class set and use it in other angular2 / typescript projects?

I have a set of classes and interfaces.

Folder structure

-Myutilityservice --classes --- SankeyUtilService.ts

There is a class in this .ts file

export class SankeyUtilService { // code goes here.. } 

This project will be pulled to another via npm / bower. I want my classes to be available in other projects through the import statement.

Like this

 import {SankeyUtilService} from 'Myutilityservice/classes'; 

How to make such a configuration? Please provide some examples.

0
source share
1 answer

You can present the project as an npm package by following these steps:

1) Create an index.ts that will export all the necessary classes:

 export * from "./lib/Helper"; export * from "./lib/Log"; // .... export * from "./lib/Query"; 

2) Make shure, you pass the ts file with the declarations flag, so you will get a nice d.ts file nearby.

3) In your package.json, specify transpiled index.js and index.d.ts, as in the example below:

 "main": "dist/index.js", "typings": "typings/index.d.ts", 

4) Publish your submitted sources with definitions in NPM.

After these steps, you can access your package as follows:

 import * as MyLib from 'MyLib'; let h = new MyLib.Helper(); 

As an example, you can check out the project here .

+2
source

Source: https://habr.com/ru/post/1267121/


All Articles