Effortlessly bring in Typescript namespace from specific namespace/type NPM package within a mono-repository

Instead of repeatedly copying Typescript types from one project to another, I have created a private NPM package with all the shared types under a Typescript namespace. Each project installs this NPM package if it uses the shared types.

index.d.ts

export * as v1_6 from './types/version1_6';

export as namespace SharedNamespace;

One of the projects using these types is a Serverless mono-repo structured as follows:

package.json
tsconfig.json
|_ lib
|_ services
    |_ service1
    |_ service2
    |_ service3
    |_ service4
    |_ serviceX

The main tsconfig.json is in the parent directory, and there are several serverless projects within a services sub-directory, where only service1 has the private NPM types installed.

When trying to use the namespace in the service1 project, an error occurs:

Cannot find namespace 'SharedNamespace'.

This error could be due to two reasons:

  1. The types are not in the default ./node_modules/@types location.
  2. Since the NPM module contains only Typescript namespaces, it is not explicitly imported anywhere in the project.

My question is: What is the best way to include it in our global type scope?

Initially, I tried extending the base tsconfig.json file with a custom one in the service1 directory:

{
  "compilerOptions": {
    "typeRoots": [
      "../../node_modules/@types",
      "./node_modules/@types",
      "./node_modules/@myOrg/privateTypes"
    ]
  },
  "extends": "../../tsconfig.json"
}

However, even after doing so, the same error persisted.

But when the namespace was manually imported where needed, the error disappeared. Ideally, importing it every time would not be necessary.

Update 1

If I use the following tsconfig.json:

{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@myOrg/privateTypes"
    ]
  },
  "extends": "../../tsconfig.json"
}

An error now occurs stating

Cannot find type definition file for 'types'. The file is in the program because: Entry point for implicit type library 'types'.

However, in the package.json of the private NPM module:

  "main": "",
  "types": "index.d.ts",
  "typeScriptVersion": "3.9.7",

and in the project directory:

So why can't it locate that file?

Answer №1

Dealing with the same issue today brought back memories. It's been over a year now, and I trust you've stumbled upon a solution by now.

You might want to experiment with the include feature in the tsconfig.json file:

{
  "include": [
    ".",
    "./node_modules/@myOrg/privateTypes/index.d.ts"
  ]
}

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

Access the properties of the encapsulated component in Vue 3, allowing for IDE autocomplete support

I have a vue3 component named MyButton which acts as a wrapper for the vuetify v-btn component. I am trying to figure out a way to make MyButton props inherit all of the props that v-btn has and also enable autocomplete feature in IntelliJ or VSCode. Is it ...

Upon receiving data from the Api, the data cannot be assigned to the appropriate datatype within the Angular Object

I am encountering an issue with the normal input fields on my page: https://i.stack.imgur.com/qigTr.png Whenever I click on the "+" button, it triggers an action which in turn calls a service class with simple JSON data. My intention is to set selectionC ...

Incorporating the Magick++ library into a node.js application deployed on the Heroku

I am encountering an issue with deploying my Node app to Heroku because it appears that Heroku is not compatible with Magick++. I have tried using custom build packs, but none of them seem to support Magick++. (1) Could this be the problem? (2) Is there ...

Vee-Validate: Are flags on the field value yielding undefined results? Explained with TypeScript

The documentation states that by using a flag on the value of a field, I should be able to obtain a boolean. For example: computed: { isFormDirty() { return Object.keys(this.fields).some(key => this.fields[key].dirty); } }, I am working ...

Updating dependencies in package.json without requiring installation

I have a script that automatically generates packages, with one package depending on the others. For example, if I have packages A, B, and C, C's package.json would include: { "name": "C", "version": "0.0.1" ...

Tips for properly importing types from external dependencies in TypeScript

I am facing an issue with handling types in my project. The structure is such that I have packageA -> packageB-v1 -> packageC-v1, and I need to utilize a type declared in packageC-v1 within packageA. All the packages are custom-made TypeScript packa ...

Navigating the interface types between Angular, Firebase, and Typescript can be tricky, especially when working with the `firebase.firestore.FieldValue`

I am working on an interface that utilizes Firestore timestamps for date settings. export interface Album{ album_name: string, album_date: firebase.firestore.FieldValue; } Adding a new item functions perfectly: this.album ...

Angular 4's OrderBy Directive for Sorting Results

I've been working on implementing a sorting pipe based on the code provided in this resource: The issue I'm facing revolves around handling undefined values within my data. The sorting pipe functions correctly when there are no undefined values ...

Dealing with NPM Installation Frustrations: "npm ERR! code EINVAL"

Starting off, everything is functioning smoothly on my OS X machine as usual. However, there seems to be a compatibility issue with Windows that I'm encountering. I'm curious if anyone has made any progress in tackling this type of error since st ...

Electron SerialPortIO

I'm currently attempting to run a basic electron application for reading and writing values from port COM4. I have already downloaded and installed node.js along with the serialport io using the following steps: $ git clone https://github.com/electro ...

Steps for creating a click event for text within an Ag-Grid cell

Is there a way to open a component when the user clicks on the text of a specific cell, like the Name column in this case? I've tried various Ag-Grid methods but couldn't find any that allow for a cell text click event. I know there is a method f ...

How to Hide Parent Items in Angular 2 Using *ngFor

I am dealing with a data structure where each parent has multiple child items. I am trying to hide duplicate parent items, but accidentally ended up hiding all duplicated records instead. I followed a tutorial, but now I need help fixing this issue. I only ...

Having issues with Craco not recognizing alias configuration for TypeScript in Azure Pipeline webpack

I am encountering an issue with my ReactJs app that uses Craco, Webpack, and Typescript. While the application can run and build successfully locally, I am facing problems when trying to build it on Azure DevOps, specifically in creating aliases. azure ...

Utilizing various filters and sorting options on API response within Angular 8

Upon receiving the following API response: [ { "imgPaths":[ "gallery/products/55ccb60cddb4d9bded02accb26827ce4" ], "_id":"5f3e961d65c6d591ba04f3d3", "productName":" ...

Tips for effectively utilizing the drag and drop feature with the Table Component in Ant Design

Recently, I received a new project from another team, and my client is requesting some changes to the admin page. Specifically, they want to customize the order of data pulled from the database. For instance, they would like to arrange the job positions in ...

Timber: The prompt 'timber' is nowhere to be found

I have successfully set up a Rails Application on my Ubuntu 18 machine. Now, I am eager to connect it with Forest Admin - an essential requirement for which is the installation of Node Application using npm. For this purpose, I need to install the Lumber C ...

Typescript compiler still processing lib files despite setting 'skipLibCheck' to true

Currently, I am working on a project that involves a monorepo with two workspaces (api and frontEnd). Recently, there was an upgrade from Node V10 to V16, and the migration process is almost complete. While I am able to run it locally, I am facing issues w ...

The server is unable to locate the command "wsgi serve" in the serverless environment

When attempting to start the server by running 'sls wsgi serve', I encountered an issue. Despite installing it with npm i serverless-wsgi and sls plugin install -n serverless-wsgi, the message "Successfully installed "serverless-wsgi@latest ...

Filtering data in an antd table by searching

Just starting out with React hooks, specifically using TypeScript, and I'm struggling to implement a search filter with two parameters. Currently, the search filter is only working with one parameter which is 'receiver?.name?'. However, I wo ...

Adding an object with a composite key to an IndexedDB object store is not permitted as the key already exists within the store. This limitation occurs when attempting to add an entry

I am facing an issue with my objectStore where adding an object with the same productId but a different shopName triggers an error in the transaction showing: ConstraintError: Key already exists in the object store.. This is how I initialized the objectSto ...