How to bundle up a set of Typescript classes and use it in other angular2/typescript projects?

后端 未结 1 629
旧巷少年郎
旧巷少年郎 2021-01-26 04:13

I have a set of classes and interfaces.

Folder structure

-Myutilityservice --classes ---SankeyUtilService.ts

This .ts file has a class

ex         


        
相关标签:
1条回答
  • 2021-01-26 04:33

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

    1) Create index.ts that will export all required classes:

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

    2) Make shure you transpile ts file with declarations flag enabled - so you will get nice d.ts file alongside.

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

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

    4) Publish your transpiled sources with definitions to NPM.

    After these steps you will be able to access your package like this:

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

    As an example - you can check a project here.

    0 讨论(0)
提交回复
热议问题