Effortless transfer of a module from one TypeScript file to another

I'm facing an issue with importing classes from one .ts file to another .ts file.

Here is the structure of my project:

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

I'm attempting to import print.ts into testing.ts

This is how my tsconfig.json appears:

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

The contents of my testing.ts are:

import { names } from 'testing';
console.log(names.printFirstName());

The contents of my print.ts are:

class nameCollection {
static printFirstName(){
    return 'OYEAAAH';
    };
}

export {nameCollection as names};

I am executing my tests using: node testing.js

But I encountered: https://i.sstatic.net/o4CN2.png

Appreciate any help!

Answer №1

When utilizing Typescript, it's important to understand how path mapping works for module resolution. Avoid mixing directories and files.

{
  "compilerOptions": {
    "paths": {
      "testing/*": ["src/nested/*"]
    }
  }
}

To properly import in your scripts, use

import {names} from 'testing/print';

For more information, refer to https://www.typescriptlang.org/docs/handbook/module-resolution.html

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

Utilizing local images in Angular with route navigation

I am new to Angular and currently learning about routes, but I have encountered a problem. In my application, I have defined the following routes. However, when trying to link to an image in the assets folder, I receive an error stating that there is no r ...

An issue with the animation script in Ionic

I'm having trouble converting this to an Ionic application. Here's my code sample. Can anyone help me with putting this code correctly in Ionic? I'm trying to achieve something like this example <script src="https://cdnjs.cloudflare.com ...

Show the key and value of a JSON object in a React component

When attempting to parse a JSON data file, I encountered an error message stating: "Element implicitly has an 'any' type because expression of type 'string' can't be used to the index type." The JSON data is sourced locally from a ...

Error in TypeScript: The property 'data' is not found within type '{ children?: ReactNode; }'. (ts2339)

Question I am currently working on a project using BlitzJS. While fetching some data, I encountered a Typescript issue that says: Property 'data' does not exist on type '{ children?: ReactNode; }'.ts(2339) import { BlitzPage } from &q ...

Trouble configuring a React TypeScript router has arisen

Recently, I have successfully set up multiple routers in TypeScript. However, I am facing challenges in implementing the same in a new project for some unknown reason. import React from 'react'; import Container from './Components/Containers ...

What are some ways to control providers in targeted tests using ng-mocks?

I recently started utilizing ng-mocks to streamline my testing process. However, I am struggling to figure out how to modify the value of mock providers in nested describes/tests after MockBuilder/MockRender have already been defined. Specifically, my que ...

Unspecified OrbitControls Compatibility Issue between Angular2 and Three.js

I'm running into issues trying to set up OrbitControls in my Angular2 project. I managed to display a scene with a box, but I'm struggling to move the camera. Upon investigation, I found that my OrbitComponent, responsible for defining orbit con ...

Exploring the redirection behavior of Angular authentication guards

My implementation involves an Angular authenticated guard. @Injectable({ providedIn: 'root' }) export class AuthenticatedGuard implements CanActivate, CanActivateChild { constructor(@Inject('Window') private window: Window, ...

suggestions for customizing Angular Material

The guidelines regarding materials specify that: "For any Angular Material component, you are allowed to define custom CSS for the component's host element that impacts its positioning or layout, such as margin, position, top, left, transform, and z- ...

A method to access a stored value in localStorage that does not involve utilizing the __zone_symbol__value property

I am encountering a problem with localStorage. After storing user information in localStorage, I am unable to retrieve it without using the __zone_symbol__value property of the parsed value. This is how I store data into locaStorage localStorage.setItem(& ...

Issue with the proper functionality of the this.formGroup.updateValueAndValidity() method in Angular 6

Currently, I am facing an issue where I need to add or remove validators in a formGroup's controls based on certain conditions. When I try to update the validators using `formGroup.updateValueAndValidity()` for the entire form, it does not seem to wor ...

Navigating through the keys of a parameter that can assume one of three distinct interfaces in TypeScript: a guide

Here is a function example: function myFunc(input: A | B | C) { let key: keyof A | keyof B | keyof C; for(key in input) { let temp = input[key]; console.log(temp); } } The definitions for A, B, and C are as follows: interfa ...

Having trouble importing a file in TypeScript?

I needed to utilize a typescript function from another file, but I encountered an issue: I created a file called Module.ts with the following code snippet: export function CustomDirective(): ng.IDirective { var directive: ng.IDirective = <ng.IDire ...

Creating a data type restricted to utilizing property names exclusively from a specified string union:

I have a specific Enum: enum MyEnum { optionOne = 0, optionTwo = 1, optionThree = 2, optionFour = 3, } and a related Type: export type EnumNamesList = keyof typeof MyEnum; I am looking to create a type similar to this: export type EnumDataTypes = ...

Changing an array into an object in JavaScript without rearranging the keys

I have a collection { 1: {id: 1, first: 1, last: 5} 2: {id: 2, first: 6, last: 10} 3: {id: 3, first: 11, last: 15} } My goal is to reverse the order of items without rearranging the keys so that it looks like this: { 1: {id: 3, first: 11, last: 15} 2: { ...

The parameter 'unknown[]' cannot be assigned to the type 'OperatorFunction'

component.ts initialize() method explains The error message says 'Argument of type 'unknown[]' is not assignable to parameter of type 'OperatorFunction<unknown[], unknown>'. Type 'unknown[]' does not match the si ...

CSS file not found error: ASP.NET core and Angular unable to locate stylesheet

Recently, I encountered a puzzling issue with my ASP.NET Core web application integrated with Angular. Despite having everything set up properly, the CSS failed to load upon launching the project. Upon inspecting the console, I was faced with this error me ...

Searching through all values can be done by following these steps

Need help with implementing a search feature that can search all values in Angular2. Here's the current code snippet: import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'filter' }) export class FilterPipe implem ...

Show data stored in a MongoDB collection as an HTML table by utilizing Angular CLI as the frontend interface

Hey there, I am currently diving into the world of nodejs, mongodb, and angular cli. As part of my college project, I'm facing some challenges. Specifically, I need to showcase the array data from mongodb on an existing table within the angular compon ...

"Implementing an Angular route component that adjusts based on specific

I am currently working on routing within my application for the 'feed' module. Within this feed, there are two types of posts that I need to display with a direct link to show full information. How can I ensure that the right component is opened ...