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:

https://i.sstatic.net/Ck2e9.png

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

Looking to incorporate an additional column 'LastName' that can be used for filtering in the Angular data table code. This column will be included if it is present in the data

function applyFilter(filterValue: string) { filterValue = filterValue.toLowerCase(); --- return filtered result return this.dataSet.filter( (item: any) => item.name ? item.name.toLowerCase(). ...

Getting event properties in a React component using the rest operator: A comprehensive guide

Can someone please assist me? I am new to TypeScript and struggling with how to use event props in my component. I have defined two props and need all my events as rest props. I encountered an error when trying to use my component with onClick event. The ...

Error: Selenium-Javascript tests encountering an unexpected token issue

Having worked with Selenium using Java for a long time, I decided to switch gears and start writing Selenium scripts in JavaScript. I found a helpful guide to learn JavaScript with Selenium, which you can check out here. However, when I attempted to run n ...

Unable to locate the next/google/font module in my Typescript project

Issue With Import Syntax for Font Types The documentation here provides an example: import { <font-name> } from 'next/google/font'; This code compiles successfully, but throws a "module not found" error at runtime. However, in this disc ...

Is it possible in TypeScript to change a string literal type into a number type?

Would it be feasible to develop a utility type Number<T> that can take a string literal type and convert it into a number? If conversion is not possible, should the utility return a never type? type Five = Number<'5'> // `Five` is con ...

How to send Multipart form data with a string payload

Many suggestions in regards to this issue recommend utilizing some form of FormData within nodejs for building a multipart form. However, I am seeking to achieve the same result without relying on the FormData library. Instead, I aim to use only request h ...

Personalized pagination - React data table

I'm looking to create a unique pagination design using react-table that fits my specific requirements: Currently, the default pagination I'm using looks like this: But I want it to resemble this: Here's the code I have so far: import Reac ...

The Angular TypeScript service encounters an undefined issue

Here is an example of my Angular TypeScript Interceptor: export module httpMock_interceptor { export class Interceptor { static $inject: string[] = ['$q']; constructor(public $q: ng.IQService) {} public request(config: any) ...

Problem encountered in a simple Jest unit test - Unexpected identifier: _Object$defineProperty from babel-runtime

Struggling with a basic initial test in enzyme and Jest during unit testing. The "renders without crashing" test is failing, as depicted here: https://i.stack.imgur.com/5LvSG.png Tried various solutions like: "exclude": "/node_modules/" in tsconfig "t ...

What are the best techniques for concentrating on a kendo maskedtextbox?

What is the correct way to set focus on the kendo-maskedtextbox in TypeScript after the view has initialized? The information provided in Telerik's example here is lacking in detail. ...

Is it possible to verify if an object matches a type without explicitly setting it as that type?

In order to illustrate my point, I believe the most effective method would be to provide a code snippet: type ObjectType = { [property: string]: any } const exampleObject: ObjectType = { key1: 1, key2: 'sample' } type ExampleType = typeof ...

I encountered a problem when trying to set up ngx-Spinner version 8.0.3

I need help implementing a spinner loader in my app. I have followed the instructions provided at [ https://www.npmjs.com/package/ngx-spinner ] and successfully installed it. However, when trying to import and add it to "imports", I encountered the follow ...

The TypeScript error code TS2345 indicates that the argument type 'JQueryXHR' cannot be assigned to the parameter type 'Promise<any>'

In a coding tutorial, an example code snippet demonstrates how to execute a JQuery getJSON() call and then transform the result into a Promise, which is later converted into an Observable. /// <reference path="../typings/tsd.d.ts" /> import { Compo ...

The implementation of user context failed to meet expectations in terms of writing

I need some clarification regarding userContext in react with typescript. Initially, I define it in RubroContext.tsx import { createContext, useContext } from "react"; import { RubroType1, RubroType2 } from "../Interfaces/interfaces"; ...

"Efficiently Distributing HTTP Requests Among Simultaneous Observers using RxJS

Currently, I am working on a feature in my Angular application that requires handling multiple concurrent fetches for the same data with only one HTTP request being made. This request should be shared among all the subscribers who are requesting the data s ...

What are some effective strategies for troubleshooting npm issues?

Today, I installed NodeJs and encountered errors while checking the npm version in cmd. Despite numerous attempts to troubleshoot the issue, I have been unsuccessful in resolving it. Seeking assistance to overcome these obstacles. The problematic code sni ...

The button's status changes to disabled until I click outside the input field in Angular

I am currently facing an issue with a form (heat index calculator) that requires 2 inputs - a dropdown and a button. The button is disabled when there are no inputs or if the inputs are invalid. Everything works correctly, except for the fact that even whe ...

"Yo" command in Npm encounters an error

While attempting to utilize the angular-generator, I encountered an issue when using the "yo" command which resulted in the following error: module.js:338 throw err; ^ Error: Cannot find module 'rx' at Function.Module._resolveFilen ...

What are some key indicators in the source code that differentiate TypeScript from JavaScript?

Reviewing some code on Github, I am looking for ways to quickly determine whether the script is written in JavaScript or TypeScript. Are there any simple tips or hints that can help with this? For instance, when examining an array declaration like the on ...

I possess information stored within the array object below, and I aim to transform it into a different array object format

Here is the response I received from my API: let data = [ { date: '2021-04-27', formatted_date: 'Apr 27', location: [ { date: '2021-04-27', formatted_date: 'Apr 27', countr ...