Navigating directories in TypeScript outDir

Currently, I am working on a TypeScript UI page object model testing project and my main focus is to avoid messy nested imports as much as possible.

I came across the paths object in the .tsconfig file which helped resolve the issue in .ts files. However, due to compiling into an outDir, the compiled .js files are unable to resolve the module path.

My development work happens in a directory named model and the compilation occurs in a directory called src.

This is how my .tsconfig file is set up:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": true,
    "noImplicitAny": false,
    "outDir": "src/",
    "baseUrl": "./model/",
    "paths": {
      "general.utils": [ "lib/general/general.utils" ],
      "pretest.setup": [ "lib/pretest.setup/pretest.setup" ],
      "tpapi": [ "lib/tpapi/tpapi" ],
      "*": [ "*" ]
    }
  },
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false
}

With the help of the paths configuration, I can now easily use:

import { chooseRandomValueFrom } from 'general.utils';

This import works perfectly in a .ts file within the model directory but fails when used in a .js file situated in the src directory.

The project is managed using npm and executed with the command npm test. This command involves compiling, running tests, and cleaning up the src directory afterwards.

#!/bin/bash

npm run compile:ts
node_modules/webdriverio/bin/wdio
npm run clean:ts

Answer №1

To effectively handle this situation, I've discovered a helpful method which involves creating a collector/exporter file to consolidate and export content from multiple files.

For instance, if I have a set of 5 files containing various types of utilities, I would appropriately name them and then gather them all in a single collector/exporter file named utilities.

Within the utilities file, the content would consist of lines such as:

export * from 'path/to/file1
export * from 'path/to/file2
export * from 'path/to/file3

This allows me to import exclusively from the collector/exporter file, streamlining the process.

While it may not be perfect, it is certainly more effective than taking no action at all.

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

Advantages of optimizing NodeJS/TypeScript application by bundling it with webpack

Currently, I am working on a Node/Express application and I am interested in incorporating the most recent technologies. This includes using TypeScript with node/Express along with webpack. I have a question: What advantages come with utilizing webpack t ...

What is the best way to dynamically assign formControlNames in Angular using *ngFor?

I am currently facing a challenge with setting form controls using *ngFor over objects in an Array. The number of objects in the array can vary, sometimes resulting in only 1 object while other times multiple objects are present. My specific issue revolve ...

Warning in Typescript: potential undefined access detected when strict mode is enabled

When using Typescript with "strict": true in the tsconfig.json, a common issue arises where warnings are not triggered for potentially undefined properties, as shown by this code snippet: let x: any = { test: false } let y = x.asdf // no warning issued ...

Error in Typescript for Firebase @google-cloud/firestore package: Duplicate declaration of 'DocumentData' identifier

I've been working with Firebase cloud functions in Typescript and everything has been running smoothly. In my code, I declared variables of type DocumentReference and GeoPoint, which prompted VS Code to import them. import { GeoPoint, DocumentReferen ...

Generating form groups programmaticallyORDynamically

I recently utilized angular-archwizard to implement a wizard step with *ngFor However, I encountered an issue on how to create a dynamic formGroup for each step. In the code below, I managed to create a single formGroup for all steps, but my goal is to ha ...

TypeScript: Error - .map() is an Invalid Function

I have come across numerous questions similar to mine, but the vast majority of them pertain to an Observable, which is not the issue I am facing. The code snippet in question looks like this: selectedItems: Item[] = null; selectedDate: Date = null; subm ...

What is the best way to import a TypeScript file in index.js?

I've recently developed an application using the react-express-starter template. I have a "server" directory where the backend is created by nodejs, containing an index.js file: const express = require('express'); const app = express(); c ...

Jest assertions encountering type errors due to Cypress

After using react-testing-library and @testing-library/jest-dom/extend-expect, I decided to install Cypress. However, I now face Typescript errors on all my jest matchers: Property 'toEqual' doesn't exist on type 'Assertion'. Did ...

Testing React-Redux Containers with JestIs this okay or would

Embarking on my journey in React with Redux, I am currently working on creating a TestUnit for a container that links to a dialog responsible for rendering a table. Within the container, I have: a mapStateToProps constant which provides properties for t ...

Removing empty options from a select dropdown in Angular 9

In the process of working with Angular 9, I am currently in the process of constructing a dropdown menu that contains various options. However, I have encountered an issue where there is a blank option displayed when the page initially loads. How can I eli ...

What could be causing the Google Sign-In functionality to fail in an Angular web application?

I'm currently working on implementing Google sign-in for my web app. I've been following this tutorial here. However, I'm facing an issue where the Google sign-in button is not appearing. I would like the authentication to happen at http://l ...

Error in declaring type even after including .d.ts file

I have encountered a situation where the npm package update-immutable does not come with a built-in typescript definition or a definition in @types. To resolve this issue, I created a type definition file within my project. As a result, VS Code is now able ...

A declaration file for the 'vuelidate' module could not be located

When I was following the installation instructions for Vuelidate in Vuejs (), I encountered a warning message at this line: import Vuelidate from 'vuelidate' The warning states: There seems to be an issue with finding a declaration file for t ...

There was an issue: Control with the name 'name' could not be located

Whenever I submit the form and try to go back, an error pops up saying "ERROR Error: Cannot find control with the name: 'name'". I'm not sure what I might be missing. Do I need to include additional checks? Below is my HTML file: <div ...

What is the best way to retrieve a string from a URL?

Is there a way to extract only a specific string from a URL provided by an API? For instance: I'm interested in extracting only: photo_xxx-xxx-xxx.png Any suggestions on how to split the URL starting at photo and ending at png? ...

Encountering an error when using the Vue 3 TypeScript Composition API for style binding with an asynchronous

I utilized nexttick alongside an async method to update a DOM element. However, I am encountering issues with returning the correct style type. An error message pops up stating: error TS2322: Type 'Promise<{ maxHeight: string; }>' is not ...

What is the correct way to type this?

Looking for some help with my React Component: const CustomIcon = ({ icon, ...props }: { icon: keyof IconType }) => { const Icon = Icons[icon] return <Icon {...props} /> } I'm attempting to use it like this: <CustomIcon icon={normalTe ...

What steps should I take to ensure that this test covers all possible scenarios?

I recently started learning React development and am currently exploring testing with Jest and React Testing Library (RTL). However, I'm facing challenges in achieving complete test coverage for the component code below: import { CustomCardActions, ...

Issue with 'else if' statement in React Typescript: Unneeded 'else' block following 'return' statement

I am encountering an issue with the else if statement below. Even after removing the last pure Else statement, I am still getting an error on ElseIf from Lint. How can I fix this problem? Error message: Unnecessary 'else' after 'return&apo ...

Transformed Vue code with TypeScript using a more aesthetically pleasing format, incorporating ref<number>(0)

Original Format: const activeIndex = ref<number>(0) Formatted Version: const activeIndex = ref < number > 0 Prettier Output: click here for image description Prettier Configuration: { "$schema": "https://json.schemastore.org ...