Having trouble opening Typescript files from a different project due to project references

Having trouble accessing types defined in another project within a mono-repo structure. Here's how the projects are organized:

-- packages
   -- project-a
      -- tsconfig.json
   -- project-b
      -- tsconfig.json
   -- shared
      -- src
         -- UserModel.ts
         -- index.ts
      -- tsconfig.json
   -- tsconfig.base.json
-- package.json
-- tsconfig.json (No changes made here)

tsconfig.base.json

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
      "declaration": true,
      "declarationMap": true,
      "emitDecoratorMetadata": true,
      "module": "commonjs",
      "noEmit": false,
      "composite": true
    },
    "files": [],
    "references": [{ "path": "./shared" }]
  }

Shared tsconfig.json

{
  "extends": "../tsconfig.base.json",
  "compilerOptions": {
    "outDir": "lib",
    "baseUrl": "./",
    "rootDir": "src",
    "composite": true,
    "experimentalDecorators": true,
    "declaration": true,
    "declarationMap": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/__tests__/*", "**/__e2e__/*"]
}

Project-a tsconfig.json

{
  "extends": ".. /tsconfig.base.json",
  "compileOnSave": true,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": ". /dist/out-tsc",
    "sourceMap": true,
    "downlevelIteration": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "module": "es2020",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "jsx": "react", 
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
      "@shared": ["../shared/src"]
    }

  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  },
  "references": [
    { "path": "../shared" }
  ]   
}

Despite these configurations, unable to reference UserModel.ts from the shared project in project-a. The IDE does not recognize it.

Import line in project-a:

import {UserModel} from '@shared/UserModel'; //Tried 'shared/src/UserModel' as well

How can I resolve this issue?

Answer №1

Kindly modify and test

project-a tsconfig.json

    "paths": {
      "@shared/*": ["packages/shared/src/*", "packages/shared/src"]
    }

Answer №2

I managed to successfully compile the folder structure by implementing the following:

packages/project-a/tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@shared/*": ["../shared/src/*"]
    }
  }
}

packages/shared/tsconfig.json

The tsconfig file in the shared package did not require any actual content for successful compilation.

{}

tsconfig.json

{
  "files": [],
  "references": [
    { "path": "packages/project-a" },
    { "path": "packages/shared" }
  ]
}

Both my IDE and the TypeScript CLI (using npx tsc --build) handled this setup without any issues.

If you're facing a specific problem, perhaps this can serve as a helpful starting point?

You may also find this Stack Overflow post useful:

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

Issue: Map container not located when implementing a leaflet map with Angular

Here is the complete error message: core.js:6479 ERROR Error: Map container not found. at NewClass._initContainer (leaflet-src.js:4066) at NewClass.initialize (leaflet-src.js:3099) at new NewClass (leaflet-src.js:296) at Object.createMap [a ...

Implement static backgrounds on images within an Angular application

I am new to using Angular 7 and I have hit a roadblock. I need help understanding how to resize images so that either the height is 270 and the width is less than 470, or the width is 470 and the height is less than 270. Once resized, I want to place these ...

Default scope injection in Angular service

Recently, I encountered an issue while working on a project in Angular involving the injection of the router. Here is a snippet from my code: import {Component, OnInit} from "@angular/core"; import {ActivatedRoute, Router} from "@angular/router"; @Compon ...

Is it possible to drive without nest.js?

I currently have a node-ts-express-setup that does not utilize nest.js. Unfortunately, the documentation and examples for drivine do not provide setup instructions without nest.js. Is there a way to use drivine without having to include nest as a dependen ...

Encountering a "Duplicate identifier error" when transitioning TypeScript code to JavaScript

I'm currently using VSCode for working with TypeScript, and I've encountered an issue while compiling to JavaScript. The problem arises when the IDE notifies me that certain elements - like classes or variables - are duplicates. This duplication ...

When attempting to pass an array of objects to a child component, TypeScript raises an error regarding types

Hey everyone, I'm currently facing an issue while trying to pass an array of objects as props. Here's the content of my data.json file: [ { "category": "Reaction", "score": 80, "icon": " ...

Exploring the world of Ionic and Angular on Coursera

I've been following a Coursera tutorial, but I've encountered an error. Although I have the code provided by the teacher, it's not working as expected. Unfortunately, I am unsure of what additional code to include since everything seems to ...

What is the best way to add JavaScript sources to the startup.cs of AspNetCore?

Is there a simple way to link JS sources from a JS project located at "JSProj/src/main.js" and "JSProj/package.json" to execute in "AspNetCoreProj/startup.cs"? How can I ensure that when I run the ASP, my controller from &quo ...

Setting default values using forRoot is functioning correctly in development mode, but encountering issues in production mode (AoT)

The code runs smoothly in development mode, but encounters issues in production: Module export class SpinnerModule { static forRoot(config?: SpinnerConfig): ModuleWithProviders { return { ngModule: SpinnerModule, providers: [SpinnerServ ...

Issue: The inject() function must be activated within an injection context, however, the origin cannot be located

While utilizing angularfire's authentication service for user registration and login in my application, I encountered an error when triggering the register or sign-in method: Error: inject() must be called from an injection context Despite attempting ...

What is the correct regex expression for validating decimal numbers between 1.0 and 4.5?

I'm having trouble creating an expression to validate numbers between 1.0 to 4.5 accurately. The current expression I'm using is not working as intended: /^[1-4]{0,1}(?:[.]\d{1,2})?$/ The requirement is to only allow values between 1.0 to ...

How to dynamically change the placeholder in an Angular 2 Material Input field

Is it possible to dynamically change the text of an input placeholder? I am able to update the text using console.log, but the interface does not reflect the change. How can I make the interface recognize the updated placeholder text? document.getElemen ...

Issue: Missing provider for t specifically when running in production mode

I've been researching and found that missing modules or services could be the cause of this issue, but I have double-checked and everything seems to be in order. Here is the complete error message: vendor.96643eaf6ee79e7b894d.bundle.js:1 ERROR Error ...

What is the reason for TypeScript not throwing an error when an interface is not implemented correctly?

In my current scenario, I have a class that implements an interface. Surprisingly, the TypeScript compiler does not throw an error if the class fails to include the required method specified by the interface; instead, it executes with an error. Is there a ...

Angular 2 encountering troublesome JSON response with invalid characters

After fetching a JSON response from an external API, I noticed that one of the variable names starts with a # character, making it look like #text in the JSON structure. Unfortunately, Angular doesn't recognize this variable. Is there a way to remove ...

It appears that the MEAN stack async call is being triggered before the response has been received

Struggling with an issue in my Angular application where an async call to a node.js server is not returning the expected response, causing a delay in the login process. I've been debugging my login component code but can't pinpoint the exact prob ...

Add a new item to an array in Angular 2 when a click event occurs

I'm trying to add a new list item (which comes from an API) when a button is pressed, but I'm not sure how to do it. Can anyone provide some guidance? Here's the code: <ul> <li *ngFor="let joke of jokes">{{joke.value}}</li> ...

Module not located following the completion of project compilation

Below is the content of my package.json file: { "main": "./build/app.js", "types": "./build/app.d.ts", "scripts": { "start": "tsc && node build/app.js", "dev": "concurrently \"tsc -w \" \"nodemon ...

Why is it necessary to include the spread operator before an array of objects?

Currently delving into Angular and stumbled upon a code snippet that seems a bit cryptic to me. The function I'm working with returns an array of objects as Observable<Product[]>: connect(): Observable<Product[]> { const dataMutati ...

Angular 11 Working with template-driven model within a directive

My currency directive in Angular 8.2 formats currency fields for users by using the following code: <input [(ngModel)]="currentEmployment.monthlyIncome" currency> @Directive({ selector: '[ngModel][currency]', providers: [Curr ...