What's the best way to utilize the tsconfig.json "types" field within a monorepository setting?

Part 1 - Utilizing the "types" Field

When a TypeScript library like library A provides type definitions alongside its normal exports, it looks like this:

declare global {
  function someGlobalFunction(): void;
}

Library B depends on the type definitions from library A. To make this work, we include the following in the tsconfig.json of library B:

"types": ["library-a"],
  • By doing this, any usage of someGlobalFunction will no longer throw a compiler error.
  • Naturally, "library-a" needs to be listed as a dependency in the package.json of library B.
  • Furthermore, "library-a" must be installed and accessible in the "node_modules" subdirectory.

Part 2 - Handling a Monorepo

In my TypeScript monorepo (using NX and Yarn), the same setup from part 1 needs to be replicated. However, there are some challenges when dealing with the 'types' field in conjunction with the 'paths' field in the tsconfig.json file used for inter-package imports within the monorepo.

Here's where it gets tricky:

  • Specifying "types": ["library-a"] will prompt TypeScript to report that it can't find "library-a".
  • Adding a typeRoots field to point to the correct "dist" directory may resolve the previous error, but it could lead to "not found" errors for individual symbols within "library-a".
  • Simply installing "library-a" directly might seem like a quick fix, but it defeats the purpose of using a monorepo in the first place.

What's the correct approach for a package to import types from another package within a monorepo? Any insight or guidance on this issue is greatly appreciated.

If necessary, I can provide a concise example repository to demonstrate the problem.

Answer №1

I was able to achieve the desired outcome by configuring the tsconfig.json file in library-b like this:

{
  "compilerOptions": {
    "types": ["../library-a"],
    "typeRoots": ["../library-a/types"],
    "paths": {
      "library-a": ["../library-a/index.ts"]
    }
  }
}

For a complete reproduction, visit https://github.com/Maxim-Mazurok/ts-2libs.

If my approach is incorrect, feel free to fork it or provide your own reproduction.

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

The guide to integrating the npm package 'mysql-import' into a node.js project with TypeScript

I'm currently facing an issue while trying to import a dump to a database using node.js and the npm module 'mysql-import'. Initially, I wrote the code in JavaScript and it worked flawlessly. However, when I attempted to modify it for TypeScr ...

hide elements only when there is no string to display in Angular2/Typescript

As I experiment with my javascript/typescript code, I've encountered an issue where a string is displayed letter by letter perfectly fine. However, once the entire string is shown, the element containing it disappears and allows the bottom element to ...

When a const variable is declared within the composition-api setup(), it remains unchanged unless redeclared within the function scope

Being primarily a back-end developer, the front-end side of things is still relatively new to me. I'm aware that there are concepts in this domain that I haven't fully grasped yet, and despite my efforts, I'm unable to resolve a particular i ...

Is there a way to efficiently convert several strings within an object that has been retrieved from an HTTP request into another language, and subsequently save this object with the

Is there a way for me to translate some strings in an object before storing it in another http request using the Google Translate API? I am currently getting the object from one http request and saving it with a put method. How can this be achieved? servi ...

Transform array sequences into their own unique sequences

Reorder Array of List to Fit My Custom Order Current Output: [ { "key": "DG Power Output", "value": "6.00", "unit": "kWh", }, { "key": "DG Run Time", "value": "5999999952", "unit": "minutes", }, { "key": "Fuel Level (Before)", "value": "8.00" ...

Is it possible to easily organize a TypeScript dictionary in a straightforward manner?

My typescript dictionary is filled with code. var dictionaryOfScores: {[id: string]: number } = {}; Now that it's populated, I want to sort it based on the value (number). Since the dictionary could be quite large, I'm looking for an in-place ...

Calling a function within another function is not allowed in Typescript

Essentially, I have an Angular Web Page that uploads a file to the server via a POST request, which is then received by my NodeJS app. The issue arises when attempting to retrieve the file path in subirArchivo() and pass it to a function called InsertaPer ...

The Node.js server is currently offline and not functioning

Encountering an error while trying to initiate the server via command prompt. Here is the specific error message received: npm ERR! Windows_NT 10.0.14393 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Prog ...

What is the significance of incorporating react context, createContext, useContext, and useStore in Mobx?

In my Typescript application, I rely on Mobx for persistence and have created a singleton class called MyStore to manage the store: export class MyStore { @observable something; @observable somethingElse; } export myStore:MyStore = new MyStore(); ...

Angular - Ensuring service completion before proceeding with navigation

I'm currently facing an issue where I need to populate data in a service before navigating, but the navigation is happening before the data is ready. Here's the code in my service: addToken(token) { this.cookieService.set( 'token', ...

Can you explain the distinction between "parser" and "parserOptions.parser" in an ESLint configuration?

For a considerable amount of time, I have been using the TypeScript and Vue presets provided below. While it has been functional, I realize that I do not fully comprehend each option and therefore seek to gain a better understanding. Firstly, what sets apa ...

Is it no longer possible to instantiate an object using the syntax <interface>{}?

There seems to be an issue with the code snippet below when run in the TypeScript playground: interface Person { name: string; age: number; } const makePerson= function(name : string, age : number) : Person { let obj = <Person>{} ...

Display the concealed mat-option once all other options have been filtered out

My current task involves dynamically creating multiple <mat-select> elements based on the number of "tag types" retrieved from the backend. These <mat-select> elements are then filled with tag data. Users have the ability to add new "tag types, ...

The type of link component that is passed in as the 'component' prop

I have created a custom Button Function Component in TypeScript using Material-UI as the base. After styling and adding features, I exported it as my own Button. Now, when I try to use this Button component in conjunction with the Link component from react ...

Broaden the scope of the generic interface utilized within the package

Wanting to incorporate automatic behaviors in RTK Query, I decided to implement debounced mutations and handle optimistic updates before the actual mutation request is made. The implementation has been successful so far. However, I am now focusing on gett ...

Developing a versatile table component for integration

My frontend app heavily utilizes tables in its components, so I decided to create a generic component for tables. Initially, I defined a model for each cell within the table: export class MemberTable { public content: string; public type: string; // ...

Encountering an issue with TypeScript error code TS2322 when trying to assign a className to the @

Encountering a typescript error when trying to apply a className to a Box element. Interestingly, the same code works on other developers' machines with almost identical configurations. Current dependencies: "@material-ui/core": "4.11. ...

Encountering a Npm ERR! when deploying Angular 6 to Heroku due to missing Start script

I am experiencing an issue with my simple angular 6 app after deploying it to Heroku. When I check the logs using the command heroku logs, I encounter the following error: 2018-07-15T00:45:51.000000+00:00 app[api]: Build succeeded 2018-07-15T00:45:53.9012 ...

Exploring the process of incorporating types for a Vue plugin

I am currently trying to integrate a self-made plugin for Vue with TypeScript. However, when I try to use the method from my vue prototype, I encounter an issue where my method $auth is not recognized on type 'myComponent'. I have also included ...

Implementing a back button in an RTL layout with Ionic 2

Just starting an Ionic 2 app in Arabic language requires a RTL layout. I decided to go with the side menu template. Adding the following line for configuring the app to RTL perfectly changed everything's direction, except for the back button which st ...