How come the path alias I defined is not being recognized?

Summary: I am encountering error TS2307 while trying to import a file using an alias path configured in tsconfig.json, despite believing the path is correct.


The structure of directories in my Angular/nx/TypeScript project appears as follows:

project
  |- libs
  |    |- aaa
  |         |- bbb
  |              |- src
  |              |    |- lib
  |              |    |    |- ccc
  |              |    |         |- components
  |              |    |              |- ddd
  |              |    |                   |- ddd.component.ts
  |              |    |- index.ts
  |              |- tsconfig.json
  |- tsconfig.base.json

tsconfig.base.json includes global paths defined like this:

{
  ...
  "compilerOptions": {
    "rootDir": ".",
    ...
    "baseUrl": ".",
    "paths": {
      ...
      "@myproject/aaa/bbb": [
        "libs/aaa/bbb/src/index.ts"
      ],
      ...

This setup works correctly - I can import @myproject/aaa/bbb and access anything exported from index.ts. For example, if I export DddComponent (defined in ddd.component.ts) from index.ts, I can use

import {DddComponent} from '@myproject/aaa/bbb';

In tsconfig.json, my intention was to introduce aliases locally within the aaa/bbb module. With these aliases, it should be possible to directly reference any files:

{
  "extends": "../../../tsconfig.base.json",
  ...
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      ...
      "@lib/ccc/*": [
        "./src/lib/ccc/*"
      ],
      ...

Now, in index.ts, when I write

import {DddComponent} from './lib/ccc/components/ddd/ddd.component';

everything is fine. However, I prefer using my locally defined alias:

import {DddComponent} from '@lib/ccc/components/ddd/ddd.component';

(Interestingly, even WebStorm's auto-completion suggests this format when I start typing import {DddComponent}.)

Unfortunately, using the latter import statement leads to build failure:

Error: libs/aaa/bbb/src/index.ts:1:28 - error TS2307: Cannot find module '@lib/ccc/components/ddd/ddd.component' or its corresponding type declarations.

I have a strong belief that this should work, however, it currently does not and the reason eludes me. Is there a way to make my alias function as intended?

Answer №1

{
  "extends": "../../../tsconfig.base.json",
  ...
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      ...
      "@lib/ccc/*": [
        "src/lib/ccc/*" <---- updating the path
      ],
      ...

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

Can we determine the data type of a value within a class instance by utilizing a function to retrieve it?

Is it feasible to create a function that maintains typing and functions in the same way as this: class Example { someNumber:number = 1; someString:string = "test"; } const example = new Example(); const value = example.someNumber; // typ ...

Press the key to navigate to a different page

I have an input field for a search box. I want it so that when I enter my search query and press enter, the page navigates to another page with the value of the input included in the URL as a query string. How can I achieve this functionality? Thank you ...

Unable to detect tsc after installing globally within Windows Sandbox

I followed the instructions provided here to install TypeScript globally. npm install -g typescript After installing both inside vscode and outside, I encountered an issue where tsc --version does not work and shows 'tsc is not recognized'. Int ...

core.js:15723 ERROR TypeError: Unable to access the 'OBJECT' property because it is undefined

Whenever I attempt to run this function, I encounter an issue. My goal is to retrieve the latitude and longitude of an address from Google's API. This error message pops up: core.js:15723 ERROR TypeError: Cannot read property 'geometry' of ...

Mastering typing properties in React with TypeScript

Could someone please help me with this issue? I have created a basic react component. interface iRowData{ name: string } export default function ResultsSection(data: iRowData[]) { return <div>Results</div> } When I try to use it in ano ...

Navigating the pathway to retrieving variables within an Angular Component function

export class MapsComponent implements OnInit { @ViewChild('googleMap') gmapElement: any; map: google.maps.Map; data = "initialised"; ngOnInit() { var directionsService = new google.maps.DirectionsService; ...

Error: The function call does not match any of the overloads. 'VueRouter' is not recognized

I'm new to TypeScript and currently trying to implement vue-router in my project. Encountering the following errors: Error TS2769: No overload matches this call in source\app\main.ts(3,3). Overload 1 of 3, '(options?: ThisTypedCompon ...

Understanding how to infer the type of a function when it is passed as an argument

Looking at the images below, I am facing an issue with my function that accepts options in the form of an object where one of the arguments is a transform function. The problem is that while the type of the response argument is correctly inferred for the e ...

A comprehensive guide on using HttpClient in Angular

After following the tutorial on the angular site (https://angular.io/guide/http), I'm facing difficulties in achieving my desired outcome due to an error that seems unclear to me. I've stored my text file in the assets folder and created a config ...

Navigating Mixins in Ember CLI Typescript

I'm curious about the best approach for handling mixins in a typed Ember application. While removing mixins from the application is ideal, many addons do not yet support TypeScript. So, how can we effectively utilize Ember Simple Auth's applicati ...

Tips for accessing the value from a subscription within a function in Ionic 3

I am working on a function that retrieves a JSON file from a specific URL. The issue I am facing is that I am trying to access a random object from this data within the file, stored in this.data. However, when I attempt to console.log(this.data) outside of ...

Is there a way to retrieve keys of the object from this combination type?

Can someone please help me understand how to retrieve keys from this union type? The Value is currently being assigned as a never type. I would like the Value to be either sno, key, or id type Key = { sno: number } | { key: number } | { id: number }; typ ...

Please provide TypeScript code for a React wrapper function that augments a component's props with two additional functions

During the course of my project, I implemented a function wrapping React component to incorporate undo/redo functionality using keyboard shortcuts Ctrl+Z and Shift+Ctrl+Z. Here is an example: import React from 'react'; interface WithUndoRedoProp ...

React 18 update causes malfunctioning of react-switch-selector component

I'm facing an issue where the component is not rendering. I attempted to start a new project but it still didn't work. Is there a solution to fix this problem or should I just wait for an update from the original repository? Encountered Error: ...

Exploring the Benefits of Utilizing the tslint.json Configuration File in an Angular 6 Project

https://i.stack.imgur.com/j5TCX.png Can you explain the importance of having two tslint.json files, one inside the src folder and one inside the project folder? What sets them apart from each other? ...

Solving TypeScript Error in React for State Destructuring

Recently, I've been working on creating a React component for AutoComplete based on a tutorial. In my development process, I am using Typescript. However, I encountered an issue while trying to destructure the state within the render suggestions metho ...

What is the best way to perform an AJAX request in Typescript with JSON data?

Currently, I am delving into the realm of AJAX and encountering some hurdles when attempting to execute an AJAX request with parameters. Specifically, I am facing difficulties in sending JSON data: My approach involves utilizing Typescript in tandem with ...

Link the Angular Material Table to a basic array

Currently facing a challenge with the Angular Material table implementation. Struggling to comprehend everything... I am looking to link my AngularApp with a robot that sends me information in a "datas" array. To display my array, I utilized the following ...

Extending an interface in TypeScript does not permit the overriding of properties

While working with Typescript, I encountered an issue where I couldn't make a property not required when overwriting it. I have defined two interfaces: interface IField { label: string; model: string; placeholder? ...

Destructuring an object in the find method using TypeScript

I'm running into an issue with object destructuring: Property 'name' does not exist on type 'ItemType | undefined'.ts(2339) App.tsx import "./styles.css"; type ItemType = { id: number; name: string; }; export defaul ...