What is the best way to distribute multiple Typescript interfaces among different projects?

I've created a project with TypeScript interfaces exclusively. For example, I have separate files like account.ts and item.ts that solely consist of interfaces.

export interface Account
{
    name: string;
}

Now, I am wondering how I can package all these files locally to be able to npm install (from local) and access these interfaces in other projects using something similar to...

import { Account, Item } from 'all-my-interfaces';

Answer №1

I recently shared an informative piece on Medium that covers this topic.


Here's a brief guide focusing on the essentials:

> mkdir my-lib
> cd my-lib
> npm init -y
> npm i -D typescript
> touch tsconfig.json
> mkdir src
> touch src/index.ts
> touch .npmignore

Inclusions in tsconfig.json file:

{
    "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "declaration": true,
      "outDir": "./dist",
      "strict": true
    }
}

src/index.ts content:

export interface Check {
    isValid(): bool;
}

Items listed in .npmignore:

dist/

Integrating npm scripts in package.json:

"prepublishOnly": "tsc"

To publish your package:

npm publish

Utilizing your library in another project:

npm i my-lib

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging @Inputs with Angular 2's <router-outlet> component for optimal functionality

I am working on a webpage with a sub-navigation feature that displays subviews below a main view. I am looking for a way to pass an object to the subviews using the <router-outlet> so that I only need to retrieve the data once in the main component a ...

Unable to execute a join operation in TypeScript

I have an array of objects listed below var exampleArray = [{ "isAvailable": true, "receipent": [{ "id": "a6aedf0c34", "receipentName": "ABC" }, { "id": "a6aedbc34" ...

Unable to initiate npm/yarn

My journey to create a React app on Linux led me to use the following commands: npm install -g create-react-app create-react-app my_app_name npm start After executing these commands, I encountered the following message: A dilemma with the project depen ...

Deactivate the underscore and include the fiscal year in AngularJS

I am currently faced with a scenario where the back end is returning the value as follows: 123222_D1.123 However, I need to display the date from the database (12-Jun-2020) as 2020-D1.123 in a drop-down menu. Currently, I am displaying the above value i ...

Position of the item in an array that contains an array with only one item

Currently, I am utilizing the Google Maps API in Angular with TypeScript. My goal is to create a clickable map of countries that changes color when clicked. This task seems simple, but I am encountering a challenge due to the complexity of the ng2-google-m ...

Encountering a permission issue while trying to execute npm install -g @angular/cli command

I recently started using Angular and am working on a new project. However, when I try to execute the following command: npm install -g @angular/cli I encounter the error message below: npm WARN checkPermissions Missing write access to /usr/local/lib/no ...

Guide to incorporating a Crypto chart widget using Angular 11

In my application, I am looking to add a crypto chart widget for each coin. The inspiration comes from the home page of coinmarketcap.com, but I haven't been able to find any guidance on how to implement it. Currently, I have made some progress, and n ...

Issue: The specific provider for "framework:pact" was not found

Currently, I am experimenting with the use of pactJS to generate pacts for my project. The testing framework I am employing is karma / jasmine. Here is a glimpse of my package.json file: { "name": "andriod-pact", "version": "1.0.0", "description": ...

Is there an issue with npm uninstall where it does not display any errors, yet fails to properly delete the package

Having trouble removing package @rollup/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6f1f031a08060142081d0e1f071e032f5e415e415f">[email protected]</a> using npm uninstall: Here are the installed packages: npm list - ...

I am able to view the node-express server response, but unfortunately I am unable to effectively utilize it within my Angular2 promise

https://i.stack.imgur.com/d3Kqu.jpghttps://i.stack.imgur.com/XMtPr.jpgAfter receiving the object from the server response, I can view it in the network tab of Google Chrome Dev Tools. module.exports = (req, res) => { var obj = { name: "Thabo", ...

Unable to incorporate angular2-datatable into VS2015 project using npm as a package manager

I'm currently working on developing a web application using Angular 2 and ASP.NET 5. In order to create a table, I decided to install the angular2-datatable package using npm. I added the dependency below to my package.json file: "angular2-datatable" ...

What are the steps to utilize Webpack 4 for exporting vendor CSS?

I am currently working on a Vue.js project with Webpack 4. When I import bootstrap.scss into my main.js bundle in the style block of my App.vue component and then export it to main.css using the mini-css-extract-plugin, the Bootstrap (vendor) CSS gets mix ...

remove unnecessary parameters from a JavaScript object using curly braces

My query pertains to an object response below result.joblist = { "collection_job_status_list": [ { "application_context": { "application_id": "a4", "context_id": "c4" }, "creation_time": "15699018476102", "pro ...

Unable to get the Angular Formly select option to bind

I'm currently working on binding formly select type options with the following code: fieldGroup: [ { key: 'TimeOffTypeID', type: 'select', className: 'flex-40 padding-10', templateOptions ...

What are the steps for setting up live-server using npm?

Upon attempting to install npm live-server, I encountered the following errors. Can someone provide assistance? C:\Users\kacho\Desktop\Web Development\Background Gradient\background-generator>npm install -g live-server npm ...

Show the distinct values of a mat-select element that retrieves information from an Angular filtered dataSource

Working on an Angular 9 app where data is displayed using a mat-table and filtered based on certain fields. The issue I'm facing is that the dropdown menu shows duplicate values, which is expected since some values may be repeated in the dataset. The ...

Filter multiple columns in an Angular custom table with a unique filterPredicate

Looking to develop a versatile table that accepts tableColumns and dataSource as @Input(). I want the ability to add custom filtering for each table column. Currently, I've set up the initialization of the table FormGroup and retrieving its value for ...

Implement a timeout feature for npm clickhouse queries

Is there a way to set the timeout for query execution in the following code snippet? I attempted to use the timeout property, but it does not seem to be functioning as expected. const results = await clickhouse.query(query, { timeout: 50, }).toPromise(); ...

Using TypeScript to asynchronously combine multiple Promises with the await keyword

In my code, I have a variable that combines multiple promises using async/await and concatenates them into a single object const traversals = (await traverseSchemas({filename:"my-validation-schema.json"}).concat([ _.zipObject( [&quo ...

Unable to set up a proxy for an npm repository on Cloudsmith using Nexus 3

Attempting to set up a proxy with Nexus for a private repository on cloudsmith.io has encountered some challenges. While configuring the proxy with the public npm registry was successful, configuring it with the Cloudsmith repository resulted in an npm ins ...