Bringing in a class in a nested module in Typescript

I'm currently working on creating a hybrid application using Angular 1 and Angular 2 with webpack. However, I've run into an issue with a nested module.

//a.ts

module a.b.c {
   export class A {
  }
}

//b.ts

 module a.b.c {
   export class B extends A {
  }
}

While the code compiles successfully, I encounter an error stating that A is undefined. I have tried different ways of importing modules but nothing seems to resolve the issue. Any ideas on what could be causing this problem?

Answer №1

To successfully link a.ts with b.ts, ensure that you incorporate a reference to a.ts within b.ts.

Update your b.ts file as shown below:

/// <reference path="./a.ts" />
  module x.y.z {
   export class Y extends X {
     public third;
  }
}

Moreover, make sure to include a.ts before b.ts in your html document:

<script src="./a.js"></script>
<script src="./b.js"></script>

Failure to adhere to this order will result in an error message.

Answer №2

In the past, we used references for our code structure. However, after integrating webpack, it seems that using references is no longer recommended as webpack does not compile it properly.

If I simply remove the module, everything works fine. But as you mentioned, this codebase contains numerous internal modules. If there isn't a better solution, and the best practice is to eliminate the module, then perhaps that is the route we should take.

Answer №3

In order to address the challenge with webpack, it's important to note that there isn't a perfect solution available. Unless the project is massive, transitioning to external modules is often the most effective approach.


If your files predominantly follow this structure:

//a.ts

module a.b.c {
  export class A {}
}

//b.ts

module d.e.f {
  export class B extends a.b.c.A
}

It becomes relatively simpler as you can simply modify them as follows:

//a.ts

export module a.b.c {
  export class A {}
}

//b.ts

import {a} from './a';
module d.e.f {
  export class B extends a.b.c.A
}

However, if your scenario involves two files within the same namespace\internal module like a.b.c, handling this becomes more complex and might require some less elegant solutions like:

//b.ts

import {aImported} from './a';
module a.b.c {
  export class B extends aImported.b.c.A
}

In this particular resolution, the files act as external modules exporting internal ones.


Another alternative to consider is taking all existing code, compiling and concatenating it using traditional methods like Gulp or Grunt before integrating the resulting javascript file into webpack. While I have utilized this method previously, I've found it cumbersome and subsequently moved on from it. Despite its minimal impact on code modification, overall, I believe it presents more challenges compared to other solutions...

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

Changing the dropdown value by clicking the previous and next buttons in Angular 2

I need to implement a feature in Angular 2 where the default date displayed in a dropdown is the current year. Additionally, when the "Prev" button is clicked, the selected year value in the dropdown should decrease by one. // Our root app component imp ...

Struggling to configure Connected React Router correctly

As I work on updating my React, Redux, and Router versions to incorporate connected-react-router, I've encountered an issue with the root reducer and store creation process. The previous functioning reducer code looked like this: const appReducer = ...

Ways to classify the prop type of a functional component by specifying the different types of props for the FC

I have created two functional components that require different prop types. export interface OrderImageProps { orders: ICartItem[] } const OrderImage: React.FC<OrderImageProps> = (props: OrderImageProps) => { return ( <div> ...

Discovering a method to recover information obtained through intercepting an observable

I am currently working on an Angular 6 cli application where a component utilizes a service to fetch data. As part of my project, I am incorporating an ngrx store into the application. I am considering abstracting the interactions with the store within the ...

How can I verify the validity of data as true or false in Angular when a button is pressed?

I am experiencing an issue where my credentials are correct, but the program is giving me a false output. Is there a solution to fix this problem? .TS loginUser(){ if(this.uname == "admin" && this.pass == "admin"){ ...

altering the URL of an AngularJS application prior to its initial load

I have encountered an issue with the Instagram API where the result redirects to my app but Instagram does not accept URLs that include the "#" symbol. For example, http://localhost:3000/#/functionalists is not being accepted due to the "/#/". My app use ...

Validation of forms in AngularJS/HTML5 that are nested within one another

Just starting out with AngularJS and experiencing issues with HTML5 nested form validation. I currently have 2 forms; mainFrm (parent form) and stateFrm (a child form). I am struggling to validate each form within its own scope. <form id="mainFrm" ng- ...

Having trouble accessing the *.json file as it is showing a 404 File Not Found error

I've been working on integrating the openrainbow API into my AngularJS with ASP.NET MVC application. There are two options for configuring and connecting to the openrainbow API, either With AngularJS or Without AngularJS. Currently, I have successful ...

Cleanse user entry

Currently I am delving into Angular and I have put together a form that allows users to reserve a hotel. This information is then sent over to a third-party API. I grasp the concept of xss and ng-bind-html when the data is displayed on the view, but how c ...

Long Waiting Time for the Ionic 2 Splash Screen

I've had struggles with the splash screen while developing several apps using ionic 2. The splash screen seems to take ages to disappear, and I understand that it's influenced by the number of plugins used and their response time. Is there a way ...

Using the recommended Prettier plugin for TypeScript in ESLint is causing issues with the ability to use the override keyword

Software Versions: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a0f190603041e2a5d445958445a">[email protected]</a> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7000021504041915023 ...

Angular.js Form Submission Results in Error 405: POST Method is Not Permitted

Every time I attempt to submit my form, I come across a 405 error. Here is the code snippet from my controller: 'use strict'; angular. module('myApp'). component('zipPopup', { templateUrl: 'zip-popup/zip ...

What is the best way to access variables within a function from an external function in Typecript?

On my HTML page, I am invoking a method with parameters using the onchange event. Once I retrieve the value, I trigger function2 on the click of another button. I am looking to access the variable from function in function2 within the same class. My implem ...

Bidirectional data binding in Angular 2 for the class attribute

Utilizing twitter bootstrap tabs, I aim to monitor the application of the active class on the li tag as users switch between different tabs. My objective is to control tab activation through custom buttons by modifying the class attribute to activate direc ...

Dropdown box not displaying any choices

I developed a basic reusable component in the following way: Typescript (TS) import {Component, Input, OnInit} from '@angular/core'; import {FormControl} from '@angular/forms'; @Component({ selector: 'app-select', templa ...

What is the best way to leverage Typescript's declaration merging along with an interface imported from an external module?

https://www.typescriptlang.org/docs/handbook/declaration-merging.html The hyperlink provided above offers insights into declaration merging using interfaces. It's something I'm interested in exploring further, particularly with interfaces that h ...

Struggling to click on a link while viewing the site on your mobile device?

Recently dove into the world of implementing mobile responsive design for a website I've been working on. While conducting some testing, I observed that the main tabs which direct users to different sections of the site, normally easy to click on desk ...

Utilizing rxjs operators on an individual formcontrol within a formgroup: A step-by-step guide

formData = Form.build({ type: [''], value:[''] searchQuery:[''] }); this.formData.valueChanges.subscribe(changes=>{ console.log(changes); //execute backend logic here }); I am interested in implementing ...

Cypress tests are not passing in CI as the Chrome Renderer keeps crashing on drone

We utilize drones to run Cypress tests on our Angular 7 application, however, we are encountering failures due to Chrome Renderer crashes in the CI environment. Surprisingly, these failures occur despite the tests running smoothly on local machines. The te ...

experiencing an excessive amount of rerenders when trying to utilize the

When I call the contacts function from the main return, everything seems fine. However, I encounter an error at this point: const showContacts = React.useCallback( (data: UsersQueryHookResult) => { if (data) { return ( < ...