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

Error messages encountered following the latest update to the subsequent project

Recently, I upgraded a Next project from version 12 to 14, and now I'm encountering numerous import errors when attempting to run the project locally. There are too many errors to list completely, but here are a few examples: Import trace for requeste ...

Having trouble getting the npm package with @emotion/react and vite to function properly

Encountering an issue with the npm package dependencies after publishing, specifically with @emotion/react. This problem arose while using vite for packaging. Upon installing the package in another project, the css property appears as css="[object Ob ...

Error: nodemon is non-existent

Instructions for installing node on a Mac using Homebrew After successfully installing nodemon with the command: npm i -g nodemon Encountered an issue where nodemon was not recognized and displayed message: command not found Tried adding the following p ...

What is the best way to modify the disabled attribute?

After disabling a button using a boolean variable, updating the variable does not remove the disabled attribute. How can I update my code to enable the button when the variable changes? Here is my current code snippet: var isDisabled = true; return ( ...

The creation script in create-react-app automatically includes an external import in the CSS file

I am facing an issue with my create-react-app. Everything works perfectly fine when I run it using npm run start. However, after building it with npm run build, some CSS appears to be missing from the app. Upon investigation, I discovered that the file bu ...

Is it advisable to use npm devDependencies in a production environment?

While reviewing the package.json file for one of our products at work, I noticed that the SDK uses socket.io for a crucial function even though socket.io-client is listed as a devDependency. Despite this discrepancy, the SDK works flawlessly for our clie ...

Sending data to Dialog Component

While working on implementing the dialog component of material2, I encountered a particular issue: I am aiming to create a versatile dialog for all confirmation messages, allowing developers to input text based on business requirements. However, according ...

Issue: 'node' is not being recognized when attempting to execute the file using the package.json script

Currently diving into the world of Node.js, I encountered an issue stating "node is not recognized as an internal or external command" whenever I attempt to start my project using either npm start or npm run start. Strangely enough, running node index.js ...

Guide for bringing in a complete npm library into React Native beyond just a single file access

Currently, I am facing an issue with importing the sjcl library into my project. Even though there are multiple files within the library, I am only able to import one file successfully. This is what I have tried from the command line: npm install sjcl -- ...

Where should Babel and Webpack be placed in your package.json file - under devDependencies or Dependencies?

I'm a beginner when it comes to npm and I have doubts on what should be included in dependencies as opposed to devDependencies. I understand that testing libraries belong in dev, but what about tools like babel and webpack? Should they also be categor ...

Configuring Typescript target and library to utilize Promise.allSettled on outdated web browsers

I am currently using TypeScript version 4.3.5 and Node.js version 14.18.1 in my project. The code I am working on is compiled to target both old and new browsers by setting target=es5 in the tsconfig file. I make use of both Promise.all and Promise.allSett ...

Looking for guidance on where to find a functional code sample or comprehensive tutorial on working with ViewMetadata in Angular2

I am currently trying to understand the relationship between viewmetadata and the fundamental use of encapsulation: ViewEncapsulation, including ViewEncapsulation.Emulated and ViewEncapsulation.None. Here is a link for further information: https://angula ...

Absolute file path reference in Node.js

I'm working on a Node.js project using WebStorm IDE. Here's the structure of my project: The root folder is named "root" and inside are 2 folders: "main" and "typings". The "main" folder has a file called "foo.ts", while the "typings" folder co ...

Encountering problems while setting up React Native, receiving warnings when running the command "npm install -g

Attempting to set up the development environment for React Native by installing Expo CLI is proving challenging. Whenever I type "npm install -g expo-cli" into my terminal, I encounter this warning: npm WARN EBADENGINE required: { node: '>=12 < ...

Encountering a Typescript issue when trying to access props.classes in conjunction with material-ui, react-router-dom

I could really use some help with integrating material-ui's theming with react-router-dom in a Typescript environment. I'm running into an issue where when trying to access classes.root in the render() method, I keep getting a TypeError saying &a ...

Challenges encountered when setting up Cordova and Ionic

After encountering an error while trying to install Cordova, a message appeared stating that PowerShell is not recognized. What could this mean and how can it be resolved? Several attempts have been made, such as adjusting the path in System variables &g ...

Encountering difficulty in reaching the /login endpoint with TypeScript in Express framework

I'm currently working on a demo project using TypeScript and Express, but I've hit a roadblock that I can't seem to figure out. For this project, I've been following a tutorial series from this blog. However, after completing two parts ...

Obtaining attribute data value upon selection change in Angular 4

Having trouble retrieving the value from data-somedata in my code... <select class="form-control input-sm" [(ngModel)]="o.id" formControlName="optionals" (change)="menuChange($event)"> <option *ngFor="let menu_optional of menu_optionals" value= ...

Unable to retrieve the request body with bodyParser

I am currently working on a NextJS React Application. Within my server/index.tsx file, I have the following code: import next from 'next'; import express from 'express'; import compression from 'compression'; import bodyParser ...

Unlock the potential of Power BI with this step-by-step guide on enhancing the Circle Card visual by incorporating unique formatting

Power BI Tutorial: Adding Formatting Options to the Circle Card Visual After completing step 8, I copied the code into my VS Code and encountered 2 error messages: Error message: "import VisualSettings - Module '"./settings"' has no e ...