Distinguishing between type definitions for peer packages in TypeScript monorepos: a comparison of development and production

In my monorepo project, I utilize yarn workspaces and lerna, with all components written in TypeScript. Each module is housed in subfolders like packages/module-n, and every module contains its own package.json file with the following entries:

"main": "dist/index.js",
"types": "dist/index.ts",

The source code for each package resides in packages/module-n/src/index.ts, which is standard practice.

While building from the command line or publishing works seamlessly thanks to tsconfig.json and a build script in every package folder, everyday code editing in VSCode presents challenges, even with tsc --watch running across all packages to maintain linked dependencies.

When attempting to navigate to a definition referencing a peer package by command-clicking, VSCode directs me to

packages/package-n/dist/index.d.ts
rather than packages/package-n/src/index.ts, causing inconvenience. Moreover, during refactoring with F2, occasional modifications to dist/index.d.ts files require restarting build:watch due to errors arising from TypeScript detecting manual changes in **/dist/index.d.ts.

To address this issue, I've implemented two scripts in the root package.json. Though functional, I'm not entirely satisfied with my solution:

"use-dev-typings": "lerna exec \"replace --quiet dist\\/index\\.d\\.ts src/index.ts package.json\"",
"use-prod-typings": "lerna exec \"replace --quiet src\\/index\\.ts dist/index.d.ts package.json\""

Both scripts are utilized in the same root package.json as shown below:

"build": "yarn use-prod-typings && lerna run build",
"build:watch": "lerna run build; yarn use-dev-typings && lerna run --parallel build:watch",

The objective is to patch all packages/package-n/package.json files before initiating edits in VSCode by using

yarn build:watch</code. This process involves replacing <code>"types": "dist/index.d.ts"
with "types": "src/index.ts", resolving issues related to command+clicking and refactoring. For one-off builds (e.g., prior to publishing), "types" revert back to "dist/index.d.ts".

I am open to suggestions on alternative approaches to achieve the desired outcome. There is concern about accidentally committing "src/index.ts" or releasing a version with this value included. To avoid bloating my packages, the contents of src/*.ts are excluded from npm releases.

Answer №1

Check out the repository here: https://github.com/Izhaki/mono.ts

Utilizing yarn workspaces and integrates seamlessly with VSCode. Make sure to read through the README for clarity.

The core concept involves employing two parallel typescript configuration trees:

  • Pre-build - incorporating aliases for various environments (e.g., VSCode, tests, webpack).
  • Build - leveraging typescript 3 project references for efficient publishing practices.

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

Guidelines for establishing an NPM Typescript package during development to uphold import syntax

Currently, I am in the process of developing an NPM TypeScript package for code-splitting from my main application. Both projects are located within a shared root directory structure: - mylib - package.json - tsconfig.json - src/ - dist/ - myapp ...

Ways to compel string type or disregard type?

When using the following code snippet: <Image src={user?.profilePictureUrl} alt={user?.name} /> An error is encountered: Type 'string | null | undefined' is not assignable to type 'string | StaticImport'. Type 'undefined ...

Here is a unique version: "Dealing with Node.js ES6 (ESM) Modules in TypeScript can be tricky, especially when the TypeScript Compiler (TSC) fails to emit the

I am facing an issue while transpiling my TypeScript project to JavaScript. I have set the project to resolve as an ES6 Module (ESM) by using the "module":"ES6" configuration, but the problem persists. This is the current setup in my ...

Having trouble displaying dropdown in Angular 2 with PrimeNG

As I work on my app using PrimeNG and Angular2, I encountered a challenge with a component that is supposed to display a dropdown menu of selectable themes. Despite following the guidelines in the PrimeNG Dropdown documentation closely, I keep receiving an ...

Accurate function calls with multiple parameters in TypeScript

As a beginner in TypeScript and currently exploring its integration with AngularJS, I am facing a particular issue where the compiler is not detecting an error. In Angular, a resource provider typically includes a get() method that returns an instance of ...

Using JSX is only possible with the addition of the '--jsx' flag, specifically when the value of "jsx" is set to "react-jsx"

typescript error message: Trouble with JSX: Must use the '--jsx' flag.ts(17004) Switching tsconfig.json's jsx to react-jsx resolves the JSX issue. However, running yarn start changes it back to react-jsx again. react-scripts has been upd ...

What steps are needed to generate an Observable that contains boolean values?

I am looking to create an Observable that can emit a boolean value and be modified by a function. My attempt was: showModal$ = new Observable<boolean>(); Unfortunately, this approach did not work as expected. What I really need is for showModal$ ...

Implement a delay for a specific function and try again if the delay expires

In my TypeScript code, I am utilizing two fetch calls - one to retrieve an access token and the other to make the actual API call. I am looking to implement a 1-second timeout for the second API call. In the event of a timeout, a retry should be attempted ...

Create a new WebSocket package for Node.js that allows for segregating connections into

I am currently exploring ways to implement a feature similar to "rooms" using the npm 'ws' package, inspired by how rooms function in socket.io. I want to avoid using socket.io, but I am faced with the challenge of retrieving user/room informatio ...

In order to iterate through a 'Set<unknown>' type, the 'downlevelIteration' flag must be used, or alternatively, the target must be set to 'es2015' or a higher version

Looking to extract all IDs from an Array and eliminate duplicates using React TypeScript. Check out my code : const uniqueMuscle = workoutexercises.map((exercise: any) => { let exerciseIds = exercise.id; exerciseIds = [...new Set(exerciseIds)]; ...

Is it necessary to also include template-only attributes in my controller's definition?

I'm working on a directive template that features a basic toggle variable. <div ng-mouseenter="$ctrl.myToggle = true" ng-mouseleave="$ctrl.myToggle = false"> ... </div> <div ng-if="$ctrl.myToggle"> ... toggled content </div> ...

Building an array from scratch in Angular

Below is the URL to access the code: https://stackblitz.com/edit/ng-zorro-antd-start-xz4c93 Inquiring about creating a new array. For example, upon clicking the submit button, the desired output should resemble the following structure: "tasks": [ { ...

What is the best way to distinguish between a button click and a li click within the same li element

In my angular and css GUI, each line is represented as an li inside a ul with a span. The functionality includes a pop-up opening when clicking on the li background and another action occurring when pressing the button (as intended). The issue arises when ...

Evaluating a method that produces an object containing a function - JavaScript testing with Jest

I'm currently dealing with a problem related to unit testing a function. The function SOP3loginConfig returns an object that contains another function called isSOP3 which returns a boolean value. I need to write tests for this specific function. The ...

Next.js 13 app directory experiences 404 Not Found error due to dynamic routing issues

I recently built a straightforward to-do app using Next.js 13 paired with TypeScript. The process involved creating an array of objects, each comprising an id string and a name string. Subsequently, I iterated through the list and showcased the names withi ...

Managing unanticipated errors in Express while utilizing async/await mechanics

Consider this TypeScript code snippet: app.get('/test_feature', function (req: Request, res: Response) { throw new Error("This is the bug"); }); app.use(logErrors); function logErrors (err: Error, req: Request, res: Response, next: NextFun ...

Ways to confirm non-null values and bypass the row if it is

I have been attempting to compare 2 dates in order to appropriately display data in a table. I have tried the following approach: ${this.dateToCompare.getTime()} > ${row.CreateDate.getTime()} However, there is an issue where CreateDate has a null value ...

What is the reason for requiring that the value type in a map must be uniform?

When using TypeScript, I expect the map type to be either a number or string, but unfortunately, an error is being reported. Click here for the Playground const map: Map<string, string | number> = new Map([ [ '1', &apo ...

The reason why Node struggles to resolve the absolute path to a directory post bundling the code using Webpack

Looking to create an AWS-CDK library using typescript and bundling it with Webpack. To include some static files from the Project root, the Copy-Plugin is used in the webpack config to move them to the dist folder. However, when referencing the imported ...

Tips for correctly specifying the types when developing a wrapper hook for useQuery

I've encountered some difficulties while migrating my current react project to typescript, specifically with the useQuery wrappers that are already established. During the migration process, I came across this specific file: import { UseQueryOptions, ...