What is the process for developing a bespoke TypeScript Declaration library and integrating it into my projects through NPM or GitHub Packages?

Project Description

I am currently developing a customized TypeScript type declaration library that will be utilized in various projects. However, I am encountering an issue when it comes to importing this TypeScript library into my projects. Although it does import, I consistently receive the error message: Module not found.

Steps Taken So Far

  • I attempted to make modifications to the package.json
  • I tried adjusting the tsconfig.json
  • I exported the type and interface from the file src/types/locationTypes.ts

Location Types (locationTypes.ts)

// Defining TypeScript Type for Latitude
export type TLatitude = number;

// Defining TypeScript Type for Longitude
export type TLongitude = number;

// Defining TypeScript Interface for Coordinates
export interface ICoordinates {
  latitude: TLatitude;
  longitude: TLongitude;
}

Main Entry Point (index.ts)

// Importing TypeScript Types
import * as dateTypes from './types/dateTypes';
import * as locationTypes from './types/locationTypes';

// Exporting Modules
export {
  dateTypes,
  locationTypes,
};

Package Configuration (package.json)

{
  "name": "@username/custom-types",
  "version": "1.0.0",
  "main": "./dist/index.ts",
  "types": "./dist/index.d.ts",
  "description": "",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/username/custom-types.git"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/username"
  },
  "author": "Jeff Lewis",
  "license": "MIT",
  "keywords": [
    "typescript",
    "types",
    "component-library",
  ],
  "scripts": {
    "build": "cd src && tsc && cp ../package.json && Echo Build completed!",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx"
  },
  "devDependencies": {
    "eslint": "^7.29.0",
    "typescript": "^4.3.4"
  }
}

TypeScript Configuration (tsconfig.json)

{
  "include": ["src/*"],
  "exclude": [
    "**/__tests__/*",
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js",
  ],
  "compilerOptions": {
    "module":  "ES6",
    "outDir": "./dist",
    "declaration": true,
    "emitDeclarationOnly": true,
    "typeRoots": ["./node_modules/@types", "./dist/index.d.ts"],
    "strict": true,
    "strictFunctionTypes": true,
  }
}

Answer №1

Make sure your src/index.ts file looks like this:

export * from'./types/dateTypes';
export * from './types/locationTypes';

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

Is there a way to halt the current traversal of visitEachChild in TypeScript Transformer API?

While navigating through each child node of a parent node using visitEachChild, is there a way to stop the process when I no longer wish to visit the subsequent child nodes? For example: Parent node Node 1 Node 2 <-- My target point. Node 3 Node 4 Nod ...

Make sure to name your Typescript component selector correctly, as it should not

As I work on my Angular project, I encountered a situation where one component needed to be referenced in the HTML of another component. To make this connection, I used kebab case for the selector like so: @Component({ selector: 'swiftlog-navbar&ap ...

The operation of fetching multiple documents within a transaction loop is not functioning as anticipated

I am encountering an issue where I am attempting to retrieve multiple documents within a transaction and then update them all in the same transaction (due to their interdependence). Despite following the rule of ensuring all reads occur before any writes, ...

Checking nested arrays recursively in Typescript

I'm facing difficulty in traversing through a nested array which may contain arrays of itself, representing a dynamic menu structure. Below is how the objects are structured: This is the Interface IMenuNode: Interface IMenuNode: export interface IM ...

Change the German number format from (0,01) to the English number format (0.01) using Angular 8

My application supports multiple languages. A user has selected German as their preferred language and I have registered it using registerLocale. I am able to convert decimal values from 0.001 (in English format) to 0,001 (in German format). However, when ...

Angular/NestJS user roles and authentication through JWT tokens

I am encountering difficulties in retrieving the user's role from the JWT token. It seems to be functioning properly for the ID but not for the role. Here is my guard: if (this.jwtService.isTokenExpired() || !this.authService.isAuthenticated()) { ...

Having trouble installing @microsoft/mgt using npm

Hello everyone, While attempting to install the MS Graph toolkit package using npm install @microsoft/mgt I am encountering an error stating that the typescript-typings package cannot be located: PS C:\Users\dev\Dev\Webpart> npm ...

The database did not respond, causing the API to resolve without sending a response for the specified endpoint (/api/dates). This could potentially lead to requests becoming stalled in Next

I have been attempting to retrieve a list of dates from my database in a straightforward manner, but unfortunately, I am not receiving any response (even after trying to log the response data to the console). The only feedback I seem to be getting when sel ...

Initial execution of Tailwind regeneration is dragging on

For my current project, I am utilizing the tailwind CLI. The issue I'm facing is that when I execute npx tailwindcss -i assets/css/input.css -o assets/css/tailwind.css --watch for the first time each day, it takes almost 15 minutes to compile. However ...

When you try to install @mui/material, it may cause a conflict with

I'm encountering an issue while trying to set up @mui/material npm install @mui/material However, during the installation process, I am getting this error message: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! ...

Error: Unable to generate MD5 hash for the file located at 'C:....gradle-bintray-plugin-1.7.3.jar' in Ionic framework

When attempting to use the command ionic cordova run android, an error occurred that prevented the successful execution: The process failed due to inability to create an MD5 hash for a specific file in the specified directory. This issue arose despite suc ...

The Network plugin is having issues with the PWA application in Ionic 4

I've been utilizing the network plugin successfully on native/Cordova devices. However, I have encountered an issue when trying to use it on a PWA app (specifically when there is no wifi connection). Can anyone shed light on why this might be happenin ...

Utilize a personalized npm script to change the name of a file

I need some help with creating a script for my angular2 project that will rename README.md to README_2.md. After installing "renamer" : "0.6.1", I tried making this script: "renameMd": "renamer --find js/README.md --replace js/README_2.md" in my package.j ...

What are some characteristics I can examine in TypeScript?

My understanding of how property checking works in TypeScript was put to the test recently. I noticed that in a specific example, checking for .bold worked fine, but when trying to check for .type, I ran into some confusion. type CustomText = { bold: ...

Retrieve the values of private variables within a defined function

While experimenting with typescript, I have encountered an issue that I can't seem to resolve. My project involves using Angular, so I will present my problem within that context. Here is a snippet of my code: class PersonCtrl{ private $scope: I ...

Having trouble loading a React component

I've been working on breaking down modules from a monolithic React project to store them separately in my npm registry. However, I'm encountering issues with exporting and importing them correctly. Previously, I was using the following code: con ...

How can a TypeScript object be declared with a single value assignment to itself?

Whenever I try to declare an object and assign a key to itself, I encounter errors. I have attempted different methods, but the error persists. const a = { d:123, a:a//<-TS2448: Block-scoped variable 'a' used before its declaration. } co ...

What is the best way to input a parameter into an https function when it is invoked from a Swift application?

Currently, integrating stripe into my app using typescript and I have the below function: exports.deleteStripeCustomer = functions.https.onCall((data, context) => { const deleted = await stripe.customers.del( "I need to add the customers ID ...

I need detailed instructions on how to handle the NPM error regarding the missing script labeled "start"

C:\Users\admin9>npm install <package.json> Syntax error in command. C:\Users\admin9>npm start npm ERR! Missing script: "start" npm ERR! npm ERR! Did you mean one of these? npm ERR! npm star # Mark your favorit ...

Observable - initiating conditional emission

How can I make sure that values are emitted conditionally from an observable? Specifically, in my scenario, subscribers of the .asObservable() function should only receive a value after the CurrentUser has been initialized. export class CurrentUser { ...